On The Road To Professional Web Development | Terminal Commands & Shell Scripting
--
*The objective of this article is to prepare for a technical interview. To succeed (1) we must be technically proficient, and (2) we must be capable of communicating our proficiency effectively.
In this post, we’ll first become conversationally fluent on the topics of terminal commands and shell scripting. Next we’ll run drills to get comfortable using them in practical ways. Finally we’ll finish the discussion with a curated list of (reportedly) common interview questions.
Part I: Terminal Commands & Shell Scripting
How Difficult Is It To Master?
Terminal commands and shell scripting let us interact with our computers using only text-based I/O. These technologies are the two key-ingredients in any shell language. Use-cases are widely varied and many won’t apply to us. By learning certain concepts and patterns of interaction, we can learn to use shells effectively without knowing every single command.
Official BASH Logo
Official PowerShell Logo
Shells: What Are They?
The relationship between a “terminal command” and “shell script” is easier shown than said. See the repl.it embed below.
On the top, you’ll see a shell script named main.sh. On the bottom, you’ll see a terminal — a text-based interface where you can enter commands. One or more commands can be stored in a shell script, which can be called from the terminal. The terminal is only a user interface, it only relates to the visual display — keyboard interactions are handled by line-editing software. In the above repl.it, it is the gnu-readline
line-editing library that enables us to use command history and keyboard shortcuts. The terminal passes commands to a shell for execution. There are many shell languages. The above example uses a terminal named xterm
to send commands to a shell named bash
on an Ubuntu Linux operating system (a UNIX-like operating system). bash
and a subset of its commands will work identically on many UNIX-like OS’s, but OS’s often include their own domain-specific commands.
Default Shells
You typically have the option to download and use your favorite shell, but the shells available to us by default depend on the operating system we’re using. bash
is the default shell on most Linux distributions. macOS used bash
until recently switching to zsh
. Windows offers its open-source PowerShell
. BSD operating systems (FreeBSD, OpenBSD, NetBSD, DragonFlyBSD) are all bash
compatible but default to ksh
or tcsh
.
What Can I Do With Terminal Commands & Shell Scripting?
- Need a program to run when a computer turns on? System boot scripts take care of this. (On Linux,
rc.local
orinit.d
let you do this, but the preferred method is to usesystemd
). - Need to periodically backup files? Scripts can automate many aspects of computer maintenance. (On Linux, running scripts at specific times or time intervals can be performed with
cron
). - Need to generate reports on the health or status of your system? (Say for example you want to know which users or shared folders occupy the most storage space). Many of the tasks we perform manually by pointing-and-clicking can be obviated when we understand what scripts are capable of.
Can I Avoid Using A Command Line Entirely?
Some of the benefits offered by using terminal commands and shell scripts can also be accomplished with general purpose programming languages. Of course, there will always be a few features relevant to your OS where using terminal commands or shell scripts are the only way. But more importantly, in the future we’ll need to interface with API’s and other developer tooling requiring familiarity and comfort with command line in a broader sense (as a mode of interacting with computers). This post focuses on the types of commands that help us navigate and monitor our machines, but if we are to become professional developers, we must become comfortable using a command line.
What Terminology Comes Up When Discussing Terminal Commands & Shell Scripting?
- A-shell (A-shell = Almquist shell) (
ash
): a variant of the Bourne Shell. “ash
is lightweight and remains popular in embedded Linux systems” - Almquist shell (see A-shell)
- Ash variants: any ash-inspired shell language. “
dash
is a popular ash variant” - Bourne shell (
sh
): the default UNIX shell in days-gone-by “Many modern shells offer a POSIX mode, forcing the shell to behave assh
” - Bourne-again shell (
bash
): a replacement for the Bourne shell. “Bash is the default interactive shell for most Linux distributions” - Bourne-style shell: any shell with syntax compatible with the Bourne shell. “Bash is a Bourne-style shell”
- C Shell (
csh
): an obsolete shell language, revolutionary in its day for ‘looking more like C’. “The improvedcsh
istcsh
” - CMD: a Windows command language. “
CMD.exe
offers a subset of the functionality of PowerShell” - Command: a directive to a computer program to perform a specific task. “we manually issue commands to bash via a terminal emulator”
- Command language: a domain-specific interpreted language for job control in computing. “Bash is an example of a command language”
- Command line (command line = command line interface = command line user environment = console = console window = teletypewriter = term = terminal = terminal application = terminal emulator = terminal window = text terminal = virtual console): a text input-output environment, typically a computer program that emulates a video terminal within some other display architecture “the default terminal on Ubuntu Linux is gnome-terminal”
- Command line interface (CLI) (see command line)
- Command line interpreter (command line interpreter = command line processor = shell = shell interpreter): a program that takes commands from the keyboard and gives them to the operating system to perform. “Shells can run scripts in the background, or be used interactively via a terminal emulator”
- Command line processor (see command line interpreter)
- Command line user environment (CLUE): (see command line)
- Command prompt: a sequence of characters used in the command line environment to indicate the readiness of the shell to accept commands. “Prompts typically end with special characters $, %, # or > and include information about the current directory”
- Console: (see command line)
- Console window: (see command line)
- Debian Almquist Shell (
dash
): - GNU (GNU’s Not UNIX!): a UNIX-like OS. GNU/Linux OS’s use the Linux kernel but the rest of the software is provided by GNU. “The GNU CoreUtils are one of several sources of domain-specific commands. GNU also pre-packages lots of other relevant tools into GNU/Linux OS’s”
- Friendly Interactive Shell (
fish
): arguably the next-most-popular interactive UNIX shell compared tobash
andzsh
, using its own unique syntax. “Made in 2005,fish
’s motto is ‘Finally, a command-line shell for the 90s’ ” - Hotkeys (hotkeys = keybindings = keyboard shortcuts): a combination of one or more keys that performs a pre-programmed action. “ctrl+u is an emacs-like keybinding used in bash to delete all characters to the left of the cursor”
- Interactive shell: a shell used via a terminal emulator. “Debian and Ubuntu Linux use
bash
as their default interactive shell” - Job control: the ability to issue commands to one or more process. “Bash gives you the ability to start, interrupt, resume, or kill processes”
- Keybindings: (see hotkeys)
- Keyboard shortcuts: (see hotkeys)
- KornShell (
ksh
): a UNIXsh
alternative. “ksh
is backward-compatible with the Bourne shell and includes many features ofcsh
” - Nano: a GNU-provided command-line text editor. “Nano is the default text editor in Ubuntu Linux”
- Non-interactive shell: a shell used to execute system scripts focusing on speed, memory efficiency and compatibility with standards. “Debian and Ubuntu Linux use
dash
as their default non-interactive shell” - POSIX (portable operating system interface): standards for OS compatibility and the basis of the SUS standard. “Like most modern shells,
bash
has a POSIX mode. When enabled, it behaves assh
” - PowerShell: a combined command language and shell scripting language for Windows. “PowerShell is compatible with
CMD.exe
commands, and more” - Prompt: (see command prompt)
- Shell: (see command line interpreter)
- Shell interpreter: (see command line interpreter)
- Shell script: a program designed to be run by a shell. “Bash scripts are a type of shell script”
- SUS (Single Unix Specification): a set of OS standards which, if followed, qualify an OS to use the UNIX trademark. “The SUS specifies certain commands that must be available in all UNIX operating systems. This is in part why Linux, macOS, BSD variants, and other UNIX-like OS’s share common commands ”
- Teletypewriter (tty): (see command line)
- TENEX C Shell (
tcsh
):csh
, improved. “tcsh
is the default shell on FreeBSD” - Term: (see command line)
- Terminal: (see command line)
- Terminal application: (see command line)
- Terminal emulator: (see command line)
- Terminal window: (see command line)
- Text terminal: (see command line)
- Text editor: a program to interactively edit plain-text. “Terminal-based text editors you’ll need to know about include nano, vim, and emacs. The ‘best editor’ is a long standing topic of heated (albeit opinion-driven) debate”
- UNIX: a family of operating systems characterized by the “UNIX philosophy” that comply with the SUS standard. “Many OS’s are based heavily on UNIX, but few can call themselves UNIX”
- UNIX-like: OS’s that don’t quite comply with the SUS standard. “Most Linux distros don’t attempt to register as SUS compliant but follow the standard closely”
- Virtual console: (see command line)
- Z Shell (
zsh
): a shell for UNIX-like OS’s incorporating select features ofbash
,ksh
, andtsch
. “zsh
is the default shell in macOS”
Part II: Using Shells
Part II focuses on using shells effectively. We have knowledge of the terminology and motivations, so the question that should be burning in our minds is how exactly these concepts are implemented. There is no substitute for experience, so we will learn-by-doing.
Essential Commands (Using Various Shells And OS’s)
Due to the various (largely similar) shell languages and OS dependency, we’ll focus on bash
on GNU/Linux. It’s a popular and acceptable path forward. That said, there are many interviewers that may expect us to be competent in additional shells and OS’s. To provide a taste of just how much “common” commands vary based on shell language and OS (and serve as a quick reference), see the below cheat-sheets:
Essential Knowledge (Using Bash On Linux)
- Warm up with this interactive tutorial to get a feel for bash
- Study this “list of basic commands” from Cardiff University
- Read this Ryan’s Tutorial on bash commands
- Read this Ryan’s Tutorial on bash scripting
- Read this Linux command-line article by Ubuntu discussing root access
- Read this guide from the guy who wrote “the book” (you may skip the book)
- Fill remaining knowledge gaps with the official bash manual
Tips & Best Practices (Using Bash On Linux)
- Memorizing bash-related keyboard shortcuts will improve your efficiency
- There exist dangerous commands you should never run
- Memorize these important job control commands
- Know these network-related commands in terminal
- Get comfortable using terminal multiplexers
- Know how to use environment variables
- Learn about bash config files and understand when and how they are used
Part III: Shell Interview Questions
The goal of Part III is to summarize interview questions from across on the web. Questions have been paraphrased to avoid duplicate entries. Drill on these while practicing for the interview, and don’t hesitate to Google solutions.
Questions Applicable To Any Shell
- compare system-defined variables to user-defined variables
- discuss arrays
- discuss background scripts
- discuss comments
- discuss conditionals
- discuss escape characters
- discuss functions
- discuss how to abort a script upon encountering an error
- discuss how to check if an instance of a program is already running
- discuss how to change the standard field separator
- discuss how to check that previous command successfully completed
- discuss how to check the status of a running process
- discuss how to convert a string to all uppercase
- discuss how to count local user accounts
- discuss how to count words in a string
- discuss how to create a file with arbitrary contents of filesize X
- discuss how to create a folder only if it does not already exist
- discuss how to create an alias
- discuss how to create an empty file
- discuss how to delete all files in a directory without prompting for confirmation
- discuss how to determine a file’s author
- discuss how to determine the length of a variable
- discuss how to determine your IP address
- discuss how to duplicate each line in a file
- discuss how to extract line X from a file
- discuss how to find all files in a directory last accessed over X days ago
- discuss how to find all files in a directory last modified over X days ago
- discuss how to find all files larger than X megabytes
- discuss how to find the total byte-size of all files in a directory
- discuss how to get the Y’th element from each line in a file
- discuss how to list all files in a directory containing a specified string
- discuss how to list all directory files sorted by name
- discuss how to list all files and hidden files in a directory
- discuss how to list all user ID’s
- discuss how to list filesize in KB, then in MB
- discuss how to list open ports
- discuss how to print all even numbers in a specified range
- discuss how to print all script arguments
- discuss how to print directory content in long or short listing format
- discuss how to replace all characters X in a string with Y
- discuss how a script can determine its own filename
- discuss how to see if a directory exists
- discuss how to select all lines in a file containing a specified string
- discuss how to show last modification date for all directory files
- discuss how to start and stop processes
- discuss how to verify a file exists in a filesystem
- discuss how to verify the most recent command has successfully completed
- discuss how to write the contents of multiple files into a single file
- discuss logical operators and comparators
- discuss loop syntax
- discuss math operators
- discuss modification of file permissions
- discuss script arguments
- discuss string concatenationcompare system-defined variables to user-defined variables
Questions Applicable To Bash On Any UNIX-Like OS
- compare
=
to==
- compare
$*
to$@
- compare
&
to&&
- compare
&
tonohup
- compare
‘
to“
- compare
stdout
tostderr
- discuss
>
- discuss
#!
- discuss
$$
- discuss
$!
- discuss
$?
- discuss
aspell
- discuss
at
- discuss
cpio
- discuss
dig
- discuss
expect
- discuss
export
- discuss
ip route show
- discuss
lspci
- discuss
netstat -nr
- discuss
patch
- discuss
ping
- discuss
ps
- discuss
route -n
- discuss
rpm -qf
- discuss
strings
- discuss
tee
- compare
${variable:-1}
to${variable: -1}
- compare
[ $x == $y ]
to[ $x -eq $y ]
- compare
[[ $string == xyz* ]]
to[[ $string == “xyz*” ]]
- discuss
echo ${new:-variable}
- discuss
name=Alex && echo ‘My name is $name’
Questions Applicable To PowerShell On Windows
- compare
.bat
to.cmd
- compare
process
toapplication
- discuss
%~d0
- discuss
ping
Closing Thoughts
I hope you’ve enjoyed this post as much as I enjoyed writing it. If you have thoughts you’d like to share, your editorial suggestions are always welcome. This post is part of a series on becoming a professional web developer. If you’d like to see more content like this, please click here to navigate to the table of contents.