Introduction

Bash is the default shell in the terminal on many Linux and UNIX based operating system, such as Ubuntu or Mac OS X. I’ve mentioned commandlinefu.com before as a great reference for learning some neat tricks with the terminal. I’ve gained a lot from the site and a few others, such as the Advanced Bash Scripting Guide and the bash hackers wiki. I wanted to share some of the tips I use most often, combined with other information that I’ve compiled through my use of the terminal.

I think I should mention that I’ve been using more applications in the terminal recently. Some people view this as backwards but I argue just the opposite. Limiting the amount of times I need to use the mouse and the number of keystrokes I need to make drastically increases my efficiency. Many bash applications are designed around single keystrokes, layered hotkeys and edit “modes”. GUIs have their place but many of my tasks can be completely more accurately and consistently via the terminal.

With that said, lets dive in.

The Basics

Hotkeys

Moving:
Ctrl + a -> Go to the beginning of the line you are currently typing on.
Ctrl + e -> Go to the end of the line you are currently typing on
Alt + f -> Move cursor forward one word on the current line.
Alt + b -> Move cursor backward one word on the current line.

Editing:
Ctrl + u -> Clears from before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + k -> Clear from after the cursor. If you are at the beginning of the line, clears the entire line.
Ctrl + w -> Delete the word before the cursor.
Ctrl + h -> Same as backspace.
Ctrl + t -> Swap the last two characters before the cursor.
Esc + t -> Swap the last two words before the cursor.

Other:
Ctrl + l -> Clear screen (same as clear command).
Ctrl + c -> Kill the current command or process.
Ctrl + z -> Puts whatever you are running into a suspended background process, fg to restore it.
Ctrl + d -> Exit the current shell.

Oddly, unlike many terminal applications, Bash hotkeys don’t make a lot of sense to me. There are very few that are “intuitive”.

History

Press the up arrow for the last command or:

!! — repeat last command

echo "hello"
!!

Outputs:

z@zentury:~$ echo "hello"
hello
z@zentury:~$ !!
echo "hello"
hello

ctrl+r is one of the best ways to search through your history. it will initialize a reverse search as you type. To go to the next result, press ctrl+r again

Advanced

History Expansion/Modification

!:0 — will repeat the first token

cd ~
ls -la
!:0

!:1-3 — defining a range: 1-3 will repeat the 2nd to 4th tokens (count starts at 0). It’s important to note that double quotes will group tokens together.

echo "hello there" && ls ~
!:3-4

!!:s/find/replace/ — will allow you to replace a part of the command

echo "hello there"
!!:s/hello/hi/

Outputs:

z@zentury:~$ echo "hello there"
hello there
z@zentury:~$ !!:s/hello/hi/
echo "hi there"
hi there

OR even shorter:

^find^replace

echo "hello there"
^hello^hi

Outputs:

z@zentury:~$ echo "hello there"
hello there
z@zentury:~$ ^hello^hi
echo "hi there"
hi there

Sequences and Pattern Expansion

Typically in a Linux or UNIX environment you have access to a command line tool name “seq” which gone over before. However, it’s good to know that bash has built-in sequence expansion and you don’t need to rely on seq.

echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z

by defining a start and end character with the ‘..’ in between, we tell bash to fill in the rest and echo a list for us. Those are all lowercase, what if you wanted uppercase? simple:

echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Or both, with a few extra characters in the mix:

echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [  ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z

It doesn’t always have to be a-z though,

echo {A..G}
A B C D E F G

This also works with numbers:

echo {0..9}
0 1 2 3 4 5 6 7 8 9
echo {0..100}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Descending as well as ascending

echo {9..0}
9 8 7 6 5 4 3 2 1 0

Echo a specific set

echo {1,4,6,9}
1 4 6 9

Applying it

Quickly backup a file

touch file1.txt
cp file1.txt{,.bak}
ls
file1.txt file1.txt.bak

explanation: the first parameter is empty, the second is .bak, this expands to >> cp file1.txt file1.txt.bak << and creates the copy

Convert an image type

If you have image magick installed, you can convert file types pretty easy using this same concept:

sudo apt-get install imagemagick

(To install on Ubuntu)

convert file.{jpg,png}

Permutations

echo {a..c}{a..c}{a..c}
aaa aab aac aba abb abc aca acb acc baa bab bac bba bbb bbc bca bcb bcc caa cab cac cba cbb cbc cca ccb ccc

Stay tuned for Part 2

These are some pretty common techniques I use to reduce the amount of typing and thinking required to complete a task in the terminal. Stay tuned for part 2 and check out some of my creative usages at commandlinefu.com.
-