Introduction To Bash
Introduction To Bash
edu/~ray/notes/bash/
Introduction
to Bash
It's a good idea to be "command-line" literate. Seriously a very good idea. But rather
than just memorizing twenty commands, you should really get into a good shell,
like Bash.
CONTENTS
Overview • Good Reads • Bash, the Shell • Reserved Words • Command Syntax and Execution • More
Overview
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:
• Arrays
• Expansions: tilde, brace, variable
• Substring awesomeness
• Conditional expressions
• Security (restricted shell mode)
• Job Control
• Timing
• Prompt customization
Good Reads
• Bash Home Page
• Bash Reference Manual READ THIS
• Bash FAQ
• Wikipedia's Bash article
• Bash Guide for Beginners
• Advantages of Bash
• Bash programming tutorial from developerWorks (pretty old) Part 1 Part 2 Part 3
• Another Bash programming introduction (older, from 2000)
• Advanced Bash Scripting Guide
• Nice Collection of Bash Examples
2 of 9 11/20/21, 20:43
Introduction to Bash https://cs.lmu.edu/~ray/notes/bash/
Examples of Shells
Organized by family:
Also see Wikipedia's Unix shell page and their Shell comparison page.
Using Bash
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
3 of 9 11/20/21, 20:43
Introduction to Bash https://cs.lmu.edu/~ray/notes/bash/
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$
triple.sh
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
fib.sh
# 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
Good to know
Make these snippets of knowledge second nature:
• The word separators (metacharacters) are these seven: ( , ) , < , > , ; , & , |
• Expansion does not occur in single quotes but it does in double quotes.
Built-in commands
Remember a command is either an executable �le or is built into the shell. These are
the builtins (as of Bash 4.2):
6 of 9 11/20/21, 20:43
Introduction to Bash https://cs.lmu.edu/~ray/notes/bash/
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.
Special Files
• If invoked as an interactive login shell
◦ First /etc/profile is executed
◦ Then the �rst �le found in the order ~/.bash_profile , ~/.bash_login ,
~/.profile is executed
Reserved Words
These can't be variables:
! time
[[ ]] { }
if then elif else fi
case esac
select in
while until for do done
function
Parses the tokens into simple and compound commands (see Shell Commands).
Performs the various shell expansions (see Shell Expansions), breaking the expanded
tokens into lists of �lenames (see Filename Expansion) and commands and
arguments. Performs any necessary redirections (see Redirections) and removes the
redirection operators and their operands from the argument list. Executes the
command (see Executing Commands). Optionally waits for the command to complete
and collects its exit status (see Exit Status).
1. Tokenizes the input into words and operators, respecting quoting, separating by
metacharacters, and applying aliases
2. Parses the tokens into commands. Then for each command:
a. Performs expansions
b. Figures out all the redirections
c. Executes the command
d. If not an asynchronous command, waits for it to �nish then records its exit
status.
More
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:
• Job Control
• Command line editing
• The history
• All of the variables
• All of the commands
• Redirection
• All forms of quoting
• Expansions
• Using regular expressions
• Here strings
8 of 9 11/20/21, 20:43
Introduction to Bash https://cs.lmu.edu/~ray/notes/bash/
9 of 9 11/20/21, 20:43