Bash Tricks


Jump to: navigation, search


Keyboard Shortcuts

Ctrl + A Go tobeginning of line
Ctrl + EGo to end of line
Ctrl + UClear all characters to the left of the cursor
Ctrl + R Search through command history
Ctrl + CKill whatever you are running
Ctrl + ZBackground current process. fg restores it.
Ctrl + WDelete the word before the cursor
Ctrl + KClear all characters to the right of the cursor
Esc + TSwap the last two words before the cursor
Alt + FMove cursor forward one word on the current line
Alt + BMove cursor backward one word on the current line
Alt + .Cycle through arguments used in previous commands

Redirecting Output

Redireting output in many cases is as simple as UNIX/DOS:

find ~ -name test >outputfile

This will only redirect the standard output, to redirect standard error:

 find ~ -name test 2>erroroutputfile

To redirect the output of both standard output AND standard error:

find ~ -name test >outputwitherrorsfile 2>&1

Eliminate 'loser takes all' Bash History

Bash only writes the command history on exiting the shell. If multiple sessions are open simultaneously the very last session to be closed will overwrite any command history from other sessions since it was opened. Confused? try the following:

  1. Opening a shell and type echo 1;echo 2 (session number 1.)
  2. Keep this session open and open another bash session (session number 2) and use the history command and you will see that the last two commands issued are not there, as the commands are only written on exiting the shell. But that it not all...
  3. Close session 1 and reopen another session (session number 3) and you will now see the echo 1;echo 2
  4. Close session number 2 and reopen another session (session number 4) and you will now see that echo 1;echo 2 has been lost!

How to fix

  1. Bash to save to the history upon every command, not when the session exits
  2. Bash must always append instead of overwriting command history.
  3. The history command must re-read the newlines upon running it, by default it only reads it when the session is started.

PROMPT_COMMAND='history -a'
shopt -s histappend
alias history='history -n;history'

Refer to previous dirs and commands

Refer to the last dir within a command with:
Insert the last command entered with:
If, for example sudo is not used when it should have been sudo !! can be used as an alternative to retyping/copying/pasting the last command.

Insert all the arguments from the previous command with


Refer to the Nth argument of the last command:
Refer to the first argument:
Refer to the last argument:

Dir navigation

cd with no arguments always goes to your home dir.
cd to last dir: cd -
Put current dir in the stack pushd . Then go back to it with popd
Go to dir, run a command and then return to the current dir: (cd dir && command)

Scrolling ls listings made easier

Instead of ls /etc|less this can be shortened to less /etc . If a folder is specified in less it will ls is and less the contents.

Simple calculator

Only useful as a simple calculator as an alternative to using a full caclualator like bc -l; fractions are decimals are not supported:

$ echo $[16*2]
$ echo $[100/11]

See Also

Personal tools