Bash, the Bourne-Again Shell, refers both to a particular Unix shell program and its associated scripting language. It is the default shell of the GNU Operating System (Linux) and Apple's OS X and is POSIX 1003.2 compliant. It is a powerful shell, and features, among other things:
pushd, popd
)
$((...))
A shell is a command interpreter. Commands can be executable files or built-ins. Commands can be bundled together into a script which a shell program executes. How the commands are packaged and wired together, using variables, functions, and control-flow operators makes up the shell's scripting language.
Organized by family:
Also see Wikipedia's Unix shell page and their Shell comparison page.
Like all shells, bash can be run interactively or non-interactively. An example interactive session:
ray@siouxsie:~$ x=4 ray@siouxsie:~$ echo x x ray@siouxsie:~$ echo $x 4 ray@siouxsie:~$ echo "hello" hello ray@siouxsie:~$ echo $PS1 \u@\h:\w\$ ray@siouxsie:~$ PS1="\w\$ " ~$ mkdir test ~$ cd test ~/test$ ls ~/test$ cat > message here is some text ~/test$ cat message here is some text ~/test$ ls message ~/test$ ls -l total 1 -rw-rw---- 1 ray ray 18 Nov 12 22:42 message ~/test$ ls -la total 9 drwxrwx--- 2 ray ray 2048 Nov 12 22:42 . drwxr-xr-x 52 ray ray 6144 Nov 12 22:42 .. -rw-rw---- 1 ray ray 18 Nov 12 22:42 message ~/test$ cp message new ~/test$ ls message new ~/test$ du 4 . ~/test$ df Filesystem 1K-blocks Used Available Use% Mounted on /dev/hda6 5044156 3518624 1269300 74% / varrun 517076 88 516988 1% /var/run varlock 517076 0 517076 0% /var/lock udev 517076 116 516960 1% /dev devshm 517076 0 517076 0% /dev/shm /dev/hda1 1011928 20 960504 1% /rescue AFS 9000000 0 9000000 0% /afs ~/test$ rm message ~/test$ help kill kill: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec] Send the processes named by PID (or JOBSPEC) the signal SIGSPEC. If SIGSPEC is not present, then SIGTERM is assumed. An argument of `-l' lists the signal names; if arguments follow `-l' they are assumed to be signal numbers for which names should be listed. Kill is a shell builtin for two reasons: it allows job IDs to be used instead of process IDs, and, if you have reached the limit on processes that you can create, you don't have to start a process to kill another one. ~/test$ y = 5 -bash: y: command not found ~/test$ y=5 ~/test$ echo x+y x+y ~/test$ echo $((x+y)) 9 ~/test$ jobs ~/test$ printf '%d + %d = %8d\n' 9 4 13 9 + 4 = 13 ~/test$ seq 4 10 3 ~/test$ seq 1 5 1 2 3 4 5 ~/test$ for w in $(seq 1 5); do echo $w; done 1 2 3 4 5 ~/test$ cat > stuff first line second line third line fourth line fourth line ~/test$ ~/test$ sort < stuff first line fourth line fourth line second line third line ~/test$ grep t stuff first line fourth line fourth line ~/test$ grep line stuff | sort | uniq first line fourth line second line third line ~/test$
And here are some example scripts:
echo Hello World
for ((c=1; c<=100; c++)); do
for ((b=1; b<=c; b++)); do
for ((a=1; a<=b; a++)); do
if [[ $(($a * $a + $b * $b)) == $(($c * $c)) ]]; then
echo $a $b $c
fi
done
done
done
# Shows fibonoacci numbers up to the first command line argument ($1)
a=0
b=1
while (( $b < $1 )); do
echo "$b"
olda=$a
a=$b
b=$(($olda + $b))
done
Make these snippets of knowledge second nature:
(
,
)
,
<
,
>
,
;
,
&
,
|
x=4
.
$x
or ${x}
.
`command args`
or $(command args)
.
Remember a command is either an executable file or is built into the shell. These are the builtins (as of Bash 4.2):
: command eval jobs read times . compgen exec kill readarray trap [ complete exit let readonly type alias compopt export local return typeset bg continue fc logout set ulimit bind declare fg mapfile shift umask break dirs getopts pushd shopt unalias builtin disown hash popd source unset caller echo help pwd suspend wait cd enable history printf test
Aren't cat
, cp
, mv
, rm
, ls
,
mkdir
, less
, find
, grep
, sed
,
cut
, ps
, chmod
, and friends part of Bash? No! These
commands live in their own executable files. In fact bash
is itself a command
just like them.
There are many places to get information on these commands, including:
At a minimum you should know these keyboard shortcuts:
Ctrl+A | Cursor to beginning of line |
Ctrl+E | Cursor end of line |
Ctrl+K | Delete to end of line |
Ctrl+_ | Undo |
Up and down arrows | Previous/next command in history |
Left and right arrows | Previous/next character on current line |
Tab | Autocompletion |
Ctrl+R | Search the history |
Ctrl+L | Clear the screen |
!! | Repeat last command |
!___ | Repeat last command beginning with ___ |
Ctrl+C | Interrupt currently running process |
But there are dozens more. See this cool reference to Bash keyboard shortcuts at SS64.com or see the chapter on command line editing in the Bash Reference Manual.
/etc/profile
is executed
~/.bash_profile
,
~/.bash_login
, ~/.profile
is executed
~/.bash_logout
is executed on logout
~/.bashrc
is executed first, then you get to interact
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
.These can't be variables:
! time [[ ]] { } if then elif else fi case esac select in while until for do done function
Bash executes scripts as follows:
For details see Chapter 3 in the Reference Manual.
These notes are only a brief introduction, so read the Bash Reference Manual. Yes, please. READ THE BASH REFERENCE MANUAL. Because this page leaves a lot out, for instance: