Shell command line essentials for engineers
TLDR: Learn a few keyboard shortcuts for BASH and get a crash course in the simplest of BASH scripting that can help speed up your command line work.
Why improve your shell skills?
As engineers, we use the command line every day. By investing just 10 minutes in better learning your tools, you can greatly improve your efficiency.
The shortcuts and basic scripting commands here can speed up the work you do and save you loads of time.
What exactly is the shell and what can you do to be more efficient?
This article assumes a basic level of familiarity with the command line. You know how to cd, ls, pwd, and mkdir.
Learning a combination of keyboard shortcuts and some light shell scripting will help you level up at the command line.
Shell vs BASH
A shell is any command line program that interacts with the underlying operating system.
BASH is a specific example of a shell program. There are other shell programs out there but BASH happens to be most commonly used. If you aren’t sure what shell you are using, it is likely BASH. To check, execute echo $SHELL
.
This article is written with BASH in mind. Most of the commands will be universal among any shell program but some parts may differ.
Shell Shortcuts
These shortcuts will need time to become a habit. Write a few of these down somewhere near your workstation that is easily visible, so over the next few weeks, you can quickly refer to those shortcuts and build this habit. It will feel a little awkward at first but in a few weeks, you’ll thank yourself for taking the time now.
If you are on Mac these shortcuts should still work using the CTRL key from the terminal.
Navigation
CTRL+A – skip cursor to the first position of the line
CTRL+E – skip cursor to the last position of the line
CTRL+LEFT/RIGHT ARROW – move cursor 1 word at a time
Can also use ALT+LEFT/RIGHT ARROW
Other
CTRL+U – clear the line you’ve typed in
CTRL+R – reverse lookup – search for a command in shell history
CTRL+L – clear the terminal – similar to the “clear” command
CTRL+K – delete from cursor to the end of line
ALT+R – undo any changes to a command fetched from history
We are assuming here that you know about TAB completion and using the up/down arrows to scroll through your command history.
A Sprinkle of Shell Scripting
Profile Script
A profile script is executed by the shell every time you start a shell session. These are used to configure settings, environment variables, and define your own custom shell built-ins.
The light scripting we will do below needs to be saved in your BASH profile script so that your shortcuts and functions are available every time you start a new shell.
Different locations
~/.profile – read by different shell programs, not just BASH
~/.bash_profile – read by login shells (Mac does this by default), use this one
~/.bashrc – is read by subshells from a login shell
If in doubt of what to use, use the ~/.bash_profile
. If you don’t see one in your home directory, make a .bash_profile
file there.
Alias
An alias is a command you define to execute another command or set of commands. Think of an alias like a shortcut on the command line.
What’s a long command you find yourself using over and over? These commands are perfect candidates to be turned into aliases.
Defining an alias
Write in your ~/.bash_profile
:
alias <my alias command>=command to execute
For example:
alias gpl='git pull
‘
Some useful aliases I like:
alias vbrc = vim ~/.bash_profile
alias sbrc = source ~/.bash_profile
alias docker_login='aws ecr get-login --registry-ids 012345678910'
Change to your ECR address – do not use any credentials in this manner
The aliases vbrc
and sbrc
are super useful when starting out customizing your profile script. These allow for quick updates to your profile script. Source means re-read this file in my current shell, otherwise, you’d have to launch a new shell for the profile to be re-read.
Bash Functions
Functions can do a lot of things. But a simple way to get started using them is to create an “alias with a variable”.
Some commands may require a value for the command to be useful. You may have discovered this when creating an alias above. In order to pass different values to our “alias” commands, we need to create a shell function instead.
Defining a shell function
In your profile script:
# This is a comment
my_function() {
echo $1
}
Now you have a function called my_function that will simply echo whatever argument you pass to it. Running my_function hello world
will echo back ‘hello world’. Parameters are represented by the $1, this is the first positional argument passed to the function.
Useful function examples
Get a Docker shell
dbash() {
docker run --rm -it --entrypoint /bin/sh $1
}
# Use it
dbash <docker image>
Get a Kubernetes shell
kbash() {
kubectl exec -it $1 -- /bin/bash -n $2
}
# Use it
kbash <pod name> <namespace>
Where to go next?
Learn more about useful shell built-ins
grep, sort, cut, uniq, sed, wc
Learn to use the man pages – man <program name>
Check out some command line cheat sheets
Continue to improve your efficiency by learning Vim
Vim Adventures – learn VIM while playing a game