Useful Bash Tricks and Commands

I spend a lot of time using the command line, and I find these features particularly useful. This might be old news to some, but they are still immensely helpful. (Also, these are extremely hard to google for, so hopefully this will save you time in more ways than one!) I tried to include as many examples as I could to make these easy to understand.

Useful Bash files

  • .bash_profile Sourced by login shell. (When using SSH)
  • .bashrc Sourced by all shells.
  • .bash_aliases Contains useful command aliases, generally sourced by ~/.bashrc
  • .bash_history Contains a history of your previous commands. (history command will list these.)

Useful history commands in Bash

!! Replay your last command

$> echo "Hi"
Hi
$> !!
echo "Hi"
Hi

!CMD Replays the last command that started with CMD.

$> ls -al
... dir listing ...
$> !ls
ls -al
... dir listing ...

!n Replays command #n in your history log

$> ls -al
... dir listing ...
$> history
... commands ...
165 ls -al
$> !165
ls -al

CMD:p Suffix used to print what would be output in the command. (:p suffix works on other commands too!)

$> ls -al
... dir listing ...
$> !ls:p
ls -al
$> touch hello.txt
$> !!:p
touch hello.txt

!$ Replaced with the last part of your last command. (can be used with :p)

$> ls -al ~/Desktop/blah
ls: /Users/rob/Desktop/blah: No such file or directory
$> mkdir -p !$
mkdir -p ~/Desktop/blah
... makes the directory ...

!^ Replaced with the first argument of the last command (can be used with :p)

$> ls -al ~/Desktop/blah
... dir listing ...
$> !^:p
ls

!* Replaced with the entire previous command, minus the program ($0) (can be used with :p)

$> ls -a -l ~/Desktop/blah
$> !*:p
-a -l ~/Desktop/blah

^A^B Replaces “A” with “B” in the previous command. (useful for correcting typos)

$> ls ~/Desktop/blha
/Users/rob/Desktop/blha: No such file or directory
$> ^ha^ah
ls ~/Desktop/blah
... dir listing ...

{..} Repeats the command for each item in the brackets. (Ranges can work too)

$> touch hello
$> mv hello{,.old}
... moves hello -> hello.old ...
$> touch {1..10}.txt
... makes files 0.txt, 1.txt, ..., 10.txt ...
$> rm {1..10}.txt
... removes those txt files from above ...
$> touch {1..10}.{a,b,c}
... makes 1.a, 1.b, 1.c, 2.a, 2.b, ..., 10.c ...
$> rm {1..10}.{a,b}
... removes all files except 1.c, 2.c, ..., 10.c ...

Useful TAB commands in Bash

/[TAB][TAB]> Shows your directory structure (including hidden directories)

$> /[TAB][TAB]
... dir listing

STR[TAB][TAB]> Lists all commands beginning with STR

$> mk[TAB][TAB]
mkafmmap     mkdep        mkextunpack  mkfile       mkfontscale  mknod        mktemp
mkbom        mkdir        mkfifo       mkfontdir    mklocale     mksdk
...

DIR[TAB][TAB]> Lists all subdirectories in DIR

$> ~/Desktop[TAB][TAB]
blah/
...

$[TAB][TAB]> Lists all shell variables

$> $[TAB][TAB]
$IRBRC                                $SHELL
$BASH                                 $ITERM_PROFILE
$BASH_ARGC                            $ITERM_SESSION_ID
...

@[TAB][TAB]> Lists all entries from /etc/hosts

$> @[TAB][TAB]
@::1            @broadcasthost
...

Useful Keyboard Shortcuts in Bash

You can also use VI mode by putting set -o vi in ~/.bashrc

  • CTRL + a Jump to the start of the line
  • CTRL + b Move back a char
  • CTRL + c Terminate the command
  • CTRL + d Delete from under the cursor
  • CTRL + e Jump to the end of the line
  • CTRL + f Move forward a char
  • CTRL + k Delete to EOL
  • CTRL + l Clear the screen
  • CTRL + r Search the history backwards
  • CTRL + R Search the history backwards with multi occurrence
  • CTRL + u Delete backward from cursor
  • CTRL + xx Move between EOL and current cursor position
  • CTRL + z Suspend current command (bg to list backgrounded jobs, fg # to bring it back)