User Tools

Site Tools


bash - bourne again shell


!! Last command
!foo Run most recent command starting with 'foo…' (ex. !ps !mysqladmin)
!foo:p Print command that !foo would run and add it as the latest to

command history

!$ Last 'word' of last command ('/path/to/file') in the command 'ls /path/to/file | uroot' in 'mysql
!$:p Print word that !$ would substitute
!* All but first word of last command (' | 'ls | lAFh /path/to/file''
!*:p Print words that !* would substitute
^foo^bar Replace 'foo' in last command with 'bar'print the result then run. ('mysqladmni | uroot' run '^ni^in' ) results in 'mysqladmin {a b c} passes words to the command substituting a and c sequentially `cp file{.bk}` runs `cp file file.bk`)

Ctrl shortcuts

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 + t Transpose the current char with the previous
Ctrl + u Delete backward from cursor
Ctrl + w Delete backward a word
Ctrl + xx Move between EOL and current cursor position
Ctrl + x @ Show possible hostname completions
Ctrl + z Suspend/ Stop the command

Alt shortcuts

Alt + < Move to the first line in the history
Alt + > Move to the last line in the history
Alt + ? Show current completion list
Alt + * Insert all possible completions
Alt + / Attempt to complete filename
Alt + . Yank last argument to previous command
Alt + b Move backward
Alt + c Capitalize the word
Alt + d Delete word
Alt + f Move forward
Alt + l Make word lowercase
Alt + n Search the history forwards non
Alt + p Search the history backwards non
Alt + r Recall command
Alt + t Transpose the current word with the previous
Alt + u Make word uppercase
Alt + back space

Tab completion

$ 2T 1) All available commands(common)
$ (string)2T All available commands starting with (string)
$ /2T Entire directory structure including Hidden one
$ (dir)2T Only Sub Dirs inside (dir) including Hidden one
$ *2T Only Sub Dirs inside without Hidden one
$ ~2T All Present Users on system from “/etc/passwd”
$ $2T All Sys variables
$ @2T Entries from “/etc/hosts”
$ =2T Output like ls or dir

Bash configuration files

These files can be found in the user home directory:

.bash_profile sourced by login shell
.bashrc sourced by all shells
.bash_aliases should be sourced by .bashrc

Redirect and pipes

Command Description
ls images/*.png 1>file_list 
standard output (1) of this command is redirected (>) to the file named file_list
date >> txt3; cat txt3
write the date at the end of the file txt3 and print it on the console
wc -l 0<file_list
over the standard input (0), wc count's the lines in file file_list
ps aux | less
pipes the output from ps to less
ls -al 2>&1
redirect errors to standard output

Bash Scripts

Run something

for i in a b c; do $i 'hello'; done

Do something on a bunch of files

for i in *.rb; do echo $i; done

If syntax

if [ -e .ssh ]; then echo "hi"; fi

file check flags

e file exists
f regular file (non directory)
d directory
s non-zero file
x execute permission

Tips & Tricks

Avoid duplicates in your history

export HISTIGNORE="&:ls:ls *:[bf]g:exit"


In Unix scripts there is a special first line which starts with a Hash (#) which tells the system where to find the command interpreter. Like this you can launch a script without putting the command interpreter in front of the filename (i.e. ./myprog.rb vs. ruby myprog.rb).

Example ruby shebang (absolute path):


If your system supports it, you can avoid hard-coding the path to the Executable in the shebang line by using #!/usr/bin/env <name of interpreter>, which will search your path for the Executable and then execute it. This is again an example for ruby:

#!/usr/bin/env ruby
Here “2T” means Press TAB twice
/srv/ · Last modified: 2009/08/15 12:14 (external edit)