On most modern Linux distributions bash (Bourne Again Shell) is the default shell. There are a lot of features and options built into bash that will help your command line efficiency. In this article we will explore some of our favorites and give you examples to help you get started.

1. Using Key Bindings

The invention of the mouse increased user efficiency, but that only holds true in the graphical user interface. On the terminal, keyboard shortcuts are king. Bash has a lot of keyboard shortcuts (key bindings) you can use to move around the terminal easily and efficiently. If you get used to using these bindings, you will notice your productivity sky rocket.

To list all the key bindings, use the bind command with the -P option.

$ bind -P
abort can be found on "\C-g", "\C-x\C-g", "\e\C-g".
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys
arrow-key-prefix is not bound to any keys
backward-byte is not bound to any keys
backward-char can be found on "\C-b", "\eOD", "\e[D".
backward-delete-char can be found on "\C-h", "\C-?".
backward-kill-line can be found on "\C-x\C-?".
backward-kill-word can be found on "\e\C-h", "\e\C-?".
...OUTPUT TRUNCATED...

Here are some of my favorite examples of efficient key bindings in bash.

Moving the Cursor

CTRL+A = Move cursor to beginning of line
CTRL+E = Move cursor to end of line
ALT+F = Move forward a word
ALT+B = Move back a word

Editing Text

CTRL+L = Clear the screen (will not erase current command)
ALT+U = Capitalize word after cursor
ALT+L = Lower the case of the word after the cursor

Process Control

CTRL+C = Interrupt currently running process
CTRL+S = Stop output to screen
CTRL+Q = Resume output to screen
CTRL+Z = Suspend current process, use “fg” to return to it.

2. Use Past Commands with History

Bash keeps a runnig record of every command you enter. This bash history can allow you to repeat or reuse old commands or arguments.

We have written several primers on using bash history in the past such as Basics of Bash History and Getting to Know Bash History with Examples. Here we will outline some of my favorite and most used history commands.

Repeat Last Command

If you want to repeat your last command, simply use the !! designator.

[[email protected] ~]$ ifconfig eno1 | grep -w inet
inet 10.0.0.2 netmask 255.255.255.0 broadcast 10.0.0.255

[[email protected] ~]$ !!
ifconfig eno1 | grep -w inet
inet 10.0.0.2 netmask 255.255.255.0 broadcast 10.0.0.255

Repeat Last Command that Started with a Specific String

You can use the !<string> to search backwards through your search history and execute the last command ran with the specified string.

[[email protected] ~]$ ifconfig eno1 | grep -Eo '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
10.0.0.2
255.255.255.0
10.0.0.255
[[email protected] ~]$ sudo dhclient -i eno1
[sudo] password for savona:
[[email protected] ~]$ !ifconfig
ifconfig eno1 | grep -Eo '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}'
10.0.0.2
255.255.255.0
10.0.0.255

Reuse Argument from Last Command

Often you run a lot of commands against the same file or “argument”. You can retrieve the argument from the last command using the !$ or !!:1 designator.

[[email protected] ~]$ file /etc/redhat-release 
/etc/redhat-release: symbolic link to fedora-release
[[email protected] ~]$ cat !$
cat /etc/redhat-release
Fedora release 29 (Twenty Nine)

Reuse Last Command with New Argument

Above we discussed using the last argument with a new command. You can also use the last command with a new argument using the !!:0 designator.

[[email protected] ~]$ getsebool use_nfs_home_dirs
use_nfs_home_dirs --> off
[[email protected] ~]$ !!:0 use_samba_home_dirs
getsebool use_samba_home_dirs
use_samba_home_dirs --> off

3. Using Command Aliases

The alias command makes it possible to define command shortcuts. These shortcuts can point to a single command with your favorite arguments, or a long pipeline of commands.

We wrote a short tip on Using Aliases to Quickly Execute Your Favorite Commands in the past. Here we will outline the details.

I used the following command a lot to wake up a computer called mymuse on my network.

sudo ether-wake -i eno1 60:d1:1b:90:cb:52

Instead of typing that every time and having to remember the MAC address, I put it into an alias called wakemymuse.

alias wakemymuse='sudo ether-wake -i eno1 60:d1:1b:90:cb:52'

Now I can just type “wakemymuse” on the command line and run the above command. If I want to make this available to me all the time, I just put the above line into my ~/.bashrc file.

$ echo "alias wakemymuse='sudo ether-wake -i eno1 90:b1:1c:90:cb:52'" >> ~/.bashrc
$ cat ~/.bashrc
.bashrc
Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
Uncomment the following line if you don't like systemctl's auto-paging feature:
export SYSTEMD_PAGER=
alias wakemymuse='sudo ether-wake -i eno1 90:b1:1c:90:cb:52'

4. Command Substitution

Command substitution allows you to use the output of a command inside of another command. You can use command subsitiion by enclosing the command as follows:

`command`

or

$(command)

I most commonly use this to set variables for reusable data. For example, I can make the variable today hold the output of the date command.

$ today=$(date +%m-%d-%Y)
$ echo $today
02-09-2019

In the example above, the output of the date command is expanded and used as the value of the variable “today”.

It also comes in handy for loops. Let’s say you wanted to grep out all the words in the dictionary that started with the letter z and create a file named after each word.

$ for i in $(cat /usr/share/dict/words | grep "^z"); do touch $i; done
$ ls -lrt
total 0
-rw-rw-r--. 1 savona savona 0 Feb 9 12:13 z.
-rw-rw-r--. 1 savona savona 0 Feb 9 12:13 z
-rw-rw-r--. 1 savona savona 0 Feb 9 12:13 za
-rw-rw-r--. 1 savona savona 0 Feb 9 12:13 zabaglione
-rw-rw-r--. 1 savona savona 0 Feb 9 12:13 zabaione
-rw-rw-r--. 1 savona savona 0 Feb 9 12:13 zabaiones
...OUTPUT TRUNCATED...

In the example above, the cat command is expanded as input for the for loop.

While working on the command line you often jump from directory to directory. You can save yourself a lot of time by learning how to navigate using metacharacters.

Return to Home Directory

To return to your home directory, use the tilde ( ~ ).

[[email protected] TEMP]$ pwd
/home/savona/Desktop/TEMP
[[email protected] TEMP]$ cd ~
[[email protected] ~]$ pwd
/home/savona
[[email protected] ~]$

This works for any user on the system, including default system accounts. For example, let’s say you have a user named Stacy. You can quickly jump to Stacy’s home folder (if you have permissions) using the tilde followed by the user name.

$ cd ~Stacy

Most Linux systems have a system user named games. That account’s home directory is /usr/games. You can quickly jump to that directory as well.

[[email protected] ~]$ cat /etc/passwd | grep games
games:x:12:100:games:/usr/games:/sbin/nologin
[[email protected] ~]$ cd ~games
[[email protected] games]$ pwd
/usr/games

Jump to Directory Inside Your Home Directory

The tilde ( ~ ) can be used to jump to folders inside your home directory, because shell understands that the metacharacter is equal to $HOME.  So to jump to your Pictures folder just continue the path after the tilde.
$ cd ~/Pictures

Return to Previous Working Directory

You can return to the previous working directory using the hyphen-minus ( – ).

[[email protected] ~]$ pwd
/home/savona
[[email protected] ~]$ cd -
/home/savona/Desktop/TEMP
[[email protected] TEMP]$ pwd
/home/savona/Desktop/TEMP
[[email protected] TEMP]$

Navigate Up One Directory

At any place in the directory structure, you can move up one directory using the double dot ( .. ).

[[email protected] ISO]$ pwd
/home/savona/Desktop/TEMP/ISO
[[email protected] ISO]$ cd ..
[[email protected] TEMP]$ pwd
/home/savona/Desktop/TEMP

Conclusion

In this article we covered some of my favorite bash tips such as key bindings or hotkeys, reusing commands, and command substitution. There are so many tips and tricks and everyone uses the command line with their own style. Maybe you would like to create your own keybindings, or use aliases for calling commands using your favorite switches? You will eventually find a rhythm and what works for you. There are a lot more advanced options in bash, but the knowing the basics is a good foundation.

Leave us a comment and share some of your favorite tips and tricks!