Anda di halaman 1dari 45

This and many other developer references available at http:/neatinfo.

com/dev_notes/_cheat
http:/neatinfo.com/dev_notes/_cheat-sheets

Notes:
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 2 of 45

May 15, 2012


Copyright 2002-2012

Table of Contents
Table of Contents ........................................... 3
Commands & Programs - Alphabetical ......... 4

The Environment .......................................... 21


Exported variables .......................................... 21
Common shell variables .................................. 21
Regular Expressions ....................................... 21

Langauages ..................................................... 4

Commands & Programs - by Function ............ 5


Archives ........................................................... 5
Communication & Network ............................... 5
Edit & View....................................................... 5
File & Dir Management ..................................... 5
Languages ....................................................... 5
Office Utilities ................................................... 5
Printer .............................................................. 5
Process Management ....................................... 6
Search and Patterns ......................................... 6
System Administration ...................................... 6

Wild Cards ................................................... 22


Wild Strings .................................................... 22
Wild Character ................................................ 22
Match Lists ..................................................... 22

Quoting ........................................................ 23
Single Quotes ................................................. 23
Double Quotes ................................................ 23
Single Character Escape ................................ 23

I/O Redirection ............................................. 24


Output Redirection .......................................... 24
Input Redirection............................................. 25
Here Documents ............................................. 25
Pipes Connecting Commands .................... 26

Basic Directory Structure ................................ 7


Common Backup Directories ............................ 7
Log files ........................................................... 7
Spool files ........................................................ 7
Configuration files............................................. 8
Device files ....................................................... 8
Other Important files ......................................... 9
X Window files .................................................. 9

Unix Commands........................................... 27
Search Order .................................................. 27
Chmod Command ........................................... 27
Echo ............................................................... 27
Expr ................................................................ 28
False .............................................................. 30
True ................................................................ 30
Go Function .................................................... 30
redo ................................................................ 31
Kill .................................................................. 31
Shutdown ....................................................... 32
Set Command ................................................. 32
Test ................................................................ 33
Trap Command ............................................... 34

Special Characters ....................................... 10


Input/Output .................................................. 10
Preset Shell Parameters ............................... 10
Login environment .......................................... 11
Shell internal settings ..................................... 11
Process ID variables....................................... 12
ksh/bash additional features ........................... 12

Bash Shell Concepts .................................... 13


Syntax ............................................................ 13

Bash Startup & Kill ........................................ 13


Configuration Files ......................................... 13
Invoking Shell ................................................. 14
dot Command ............................................... 14
XWindow & KDE Startup ................................ 15
Background Processing .................................. 15
Terminating a Process .................................... 15
Environment ................................................... 15

Conditional Statements ................................ 34


If Statement .................................................... 34
While Statement ............................................. 36
Until Command ............................................... 37
For Command ................................................. 37
Case Command .............................................. 37

Shell Functions............................................. 38
Functions vs. Scripts ....................................... 38
Defining a Function ......................................... 38
Where to Define Functions .............................. 39
Status of Definitions ........................................ 39

Built-in Shell Commands .............................. 17


Parameters & Variables ................................ 18
Assigning a Variable ....................................... 18
Referencing a Variable ................................... 18
Special Variables............................................ 18
Position Parameters ....................................... 19
Shift Command .............................................. 19
Set Command ................................................ 19
Conditional Use of Variables........................... 20

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Search Order................................................ 39
Command Line Shell Invoking ...................... 40
Invoking .......................................................... 40

Debugging.................................................... 41
Linux Bash Command Reference Sheet....... 42

Pg 3 of 45

May 15, 2012


Copyright 2002-2012

Commands & Programs - Alphabetical


adduser
awk
at
bc
cal
calendar
cancel
cat
cd
chgrp
chmod
chown
cmask
cmp
comm
compress
cp
cpio
cron
cu
cut
date
dd
df
diff
du
echo
emacs
env
ex
eqn
find
finger
fork
fsck
ftp
grep
gzip
head
history
info
join
kill
less
ln
login
lp
lpc
lprm
lpstat
lpq
ls
mail
mailx
man
mesg
mkdir
mm
more

manage user account


search & editing
job scheduler
calculator
calendar
reminder service
remove print jobs from queue
concatenates or prints files
change directory
change group permissions
change file/dir permissions
change owner permissions
sys def permission see umask
compare two text files
find same lines of sorted files
compress file
copy files
file archive program
job scheduler
call another system
cut/paste columns of text
date and time
file copy program
mounted file sys/drives info
difference between two files
file & dir disk usage
shows argument
editor
show environment variables
line-oriented text editor
format mathematical text
find and act on files
user info
create child process
file system check and fix
file transfer
search pattern and do action
compress files
show first lines of file
print last commands
like man, command info
join two columns of text
terminate or send signals
file viewer
create file links
sign on to system
line printer manager
manage print jobs
remove print jobs
status of print jobs
status of print jobs
show directory contents
manage mail
advanced mail management
manual of command info
permit or deny talk messages
create new directory
macro print formatter
file viewer, use less instead

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

mount
mv
ping
netstat
nroff
passwd
paste
pg
ping
postnews
pr
ps
pstat
pwd
rcp
readnews
rlogin
rm
rn
rmdir
rpm
script
sed
shl
sort
spell
shutdown
split
stty
tbl
tail
talk
tar
tee
telnet
time
troff
umask
umount
uncompress
uniq
uptime
uucp
uupick
uuto
vi
wc
who
write
zcat

init make file sys or device


move or rename files
transfer time & route
transfer info
print formatter, use mm instead
change login password
cut and paste columns of text
file viewer, use less instead
transfer time & route
netnews writer
format and print text file
show program status
show kernel info
print working directory
remotely copy files
netnews manager
remote login
remove files
netnews manager
remove directories
manage packages
saves bash session to file
pattern editor
shell manager
ort and merge text files
spelling checker
halt or reboot system
text editing
set terminal options
put tables into documents
view last lines of text file
talk (type) to another user
combine files
split output
net file manager
time info
typeset formatting
usr default perm see cmask
remove file system or device
expand compressed file
remove dup lines of text file
elapsed time since boot
file transfer between systems
retrieve file sent via uuto
Send file to another system
text editor
word count
who is logged into system
write to another user
display compressed file

Langauages
cc
f77
make
nasm
pc
perl

Pg 4 of 45

c compiler
fortran compiler
source code manager
assembler
pascal compiler
perl compiler

May 15, 2012


Copyright 2002-2012

Commands & Programs - by Function


Archives
compress
cpio
gzip
rpm
tar
uncompress
zcat

compress file
file archive program
compress files
manage packages
combine files
expand compressed file
display compressed file

Communication & Network


cu
ftp
getty
netstat
ping
pty
tty
rcp
rlogin
rset
stty
talk
tee
telnet
termcap
tset
uucp
uupick
uuto

call another system


net file manager
transfer info
transfer time & route

remotely copy files


remote login
set terminal options
talk (type) to another user
split output
net file manager

file transfer between systems


retrieve file sent via uuto
Send file to another system

Edit & View


awk
cat
cmp
comm
cut
diff
echo
emacs
eqn
ex
head
join
less
more
nroff
paste
pg
sed
sort
split
tail
tbl
uniq
vi

search, filter, edit


concatenates or prints files
compare two text files
find same lines of sorted files
cut & paste columns of text
difference between two files
show argument(s)
editor
format mathematical text
line-oriented text editor
show first lines of file
join two columns of text
file viewer
file viewer, use less instead
prt formatter, use mm instead
cut and paste columns of text
file viewer, use less instead
search, filter, edit
sort and merge text files
txt edit
view last lines of text file
put tables into documents
remove duplicate lines
text editor

File & Dir Management


cd
chgrp
chmod
chown

change
change
change
change

directory
group permissions
file/dir permissions
owner permissions

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

cp
dd
df
du
find
fsck
grep
ftp
ln
ls
mkdir
mount
mv
path
pwd
rm
rmdir
umount
uucp
uupick
uuto

copy files
file copy program
mounted file sys & drive info
file & dir disk usage
find and act on files
file system check and fix
search pat and act on file
file transfer
create file links
show directory contents
create new directory
make file or dev available
move or rename files
change shell environment
print working directory
remove files
remove directories
remove file system or device
file transfer between systems
retrieve file sent via uuto
Send file to another system

Languages
cc
f77
gcc
java
make
nasm
pc
perl
tk
tkl

c compiler
fortran compiler
gcc compiler
java compiler
source code manager
assembler
pascal compiler
perl compiler
tk compiler
tkl compiler

Office Utilities
bc
cal
calendar
date
mail
mailx
postnews
readnews
rn
spell
talk
time
wc
who
write

calculator
calendar
reminder service
date and time
mail managment
advanced mail management
netnews writer
netnews mgr, use rn instead
netnews manager
spelling checker
talk (type) to another user
time info
word count
who is logged into system
write to another user

Printer
cancel
lp
lpc
lprm
lpstat
lpq
mm
nroff
pr
troff

Pg 5 of 45

remove print jobs from queue


line printer manager
manage print jobs
remove print jobs
status of print jobs
status of print jobs
macro print formatter
prt formatter, use mm instead
format and print text file
typeset formatting

May 15, 2012


Copyright 2002-2012

Process Management
at
cron
date
env
export
fork
kill
ps
tee

job scheduler
job scheduler
date and time
show environment variables
send variable to shell
create child process
terminate or send signal
program status
split output

Search and Patterns


awk
find
grep
sed
sort
nohup
ps
tee

pattern scanning
find and act on files
search pat and act on file
pattern editor
sort and merge text files
prog running after logoff
program status
split output

System Administration
adduser
chgrp
chmod
chown

manage
change
change
change

user account
group permissions
file/dir permissions
owner permissions

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

cmask
date
echo
env
find
finger
grep
history
info
login
mail
mailx
man
mesg
passwd
path
pstat
script
shl
shutdown
stty
time
umount
umask
uptime
who

Pg 6 of 45

sys default perm see umask


date and time
shows argument
show environment variables
find and act on files
user info
search pattern & do actions
show command history
like man, command info
sign on to system
manage mail
advanced mail management
manual of command info
permit or deny talk messages
change login password
change shell environment
show kernel info
saves bash session to file
shell manager
halt or reboot system
set terminal options
time info
remove file system or device
usr default perm see cmask
elapsed time since boot
who is logged into system

May 15, 2012


Copyright 2002-2012

Basic Directory Structure


/ .................................
/bin ...............................
/boot ..............................
/dev ...............................
/etc ...............................
/home ..............................
/lib ...............................
/lost+found ........................
/mnt ...............................
/opt ...............................
/proc ..............................
/root .............................
/sbin ..............................
/tmp ...............................
/usr ...............................
/usr/bin ...........................
/usr/doc ...........................
/usr/man ...........................
/usr/include .......................
/usr/include/g++ ...................
/usr/lib ...........................
/usr/local .........................
/usr/sbin ..........................
/usr/src ...........................
/usr/src/linux .....................
/usr/tmp ...........................
/var ...............................
/var/tmp ...........................

;root directory
;system commands
;kernel & boot files
;device drivers
;pub system configuration files
;user private files & settings
;c compiler libraries
;files fragments are saved here
;external file systems and devices
;optional programs, i.e. KDE
;process files
;roots custom files & settings
;superuser commands needed to start
;system temporary files
;pub cmds & prop, can be read-only
;pub commands
;pub info files
;pub man files
;pub c header files
;pub c++ header files
;pub libraries
;extensions independent of distro
;superuser commands
;pub application source files
;kernel source files
;pub temporary files
;config files, link to /usr
;pub temporary files

Common Backup Directories


/etc/lilo.conf .....................
/boot/grub .........................
/etc/fstab .........................
/etc/modules.conf ..................
/etc/isapnp.conf ...................
/etc/Xll/XF86Config-4 ..............
/etc/X11/XF86Config ................
/etc/cups ..........................
/etc/printcap .....................
/etc/bashrc ........................
/etc/profile .......................
/etc/crontab .......................
/etc/rc.d ..........................
/etc/inittab .......................
/etc/ssh ...........................
/root .............................
/home ..............................

;Lilos boot loader configuration


;grubs boot loader configuration
;disk partition table configuration
;modules to load % their parameters
;ISA Plug & Play hardware parameters
;Xs settings
Xs settings
;CUPSs printer settings
;lpr print settings
;bash shell system-wide configuration
;system-wide environment
;cron jobs to be executed
;run level settings
;sets default run level & settings
;ssh settings for secure remote access
;roots custom files & settings
;users custom files & settings

Log files
/var/log

;log files

Spool files
/usr/spool
/usr/spool/lp

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

;buffer for file xfer, etc


;buffer for printer

Pg 7 of 45

May 15, 2012


Copyright 2002-2012

Configuration files
/boot/grub .........................
/etc/bashrc ........................
/etc/crontab .......................
/etc/cups ..........................
/etc/DIR_COLORS ....................
/etc/fstab .........................
/etc/group ........................
/etc/hosts ........................
/etc/initd.conf ...................
/etc/inittab .......................
/etc/isapnp.conf ...................
/etc/lilo.conf .....................
/etc/modules.conf ..................
/etc/passwd .......................
/etc/printcap .....................
/etc/profile .......................
/etc/rc.config .....................
/etc/rc.d ..........................
/etc/shadow .......................
/etc/ssh ...........................
/etc/syslogd.conf .................
/etc/XF86Config ....................
/etc/X11/XF86Config ................
~/home/.profile ....................
/sbin/init.d .......................

;grubs boot loader configuration


;bash shell system-wide configuration
;cron jobs to be executed
;CUPSs printer settings
;ls color settings
;disk partition table configuration
;user groups
;hostname to address table
;net services, ftp, telnet, etc
;sets default run level & settings
;ISA Plug & Play hardware parameters
;lilos boot loader configuration
;modules to load & their parameters
;usr info, shell pref, home dir, etc
;lpr print settings
;shells login script
;sys config settings read during boot
;run level settings
;passwords
;ssh settings for secure remote access
;syslog daemon settings
;X window settings
;Xs settings
;usr custom environment settings
;scripts for system start

Device files
/dev/fd0fd1 .......................
/dev/hda ...........................
/dev/hda1hda15 ....................
/dev/hdb ...........................
/dev/hdb1hdb15 ....................
/dev/sda ...........................
/dev/sda1sda15 ....................
/dev/sdb ...........................
/dev/sdb1sdb15 ....................
/dev/sdc... ........................
/dev/cdrom .........................
/dev/hda - hdd .....................
/dev/scd0 scd1 ...................
/dev/rmt0 ..........................
/dev/nrmt0 .........................
/dev/ftape .........................
/dev/ntape .........................
/dev/mouse .........................
/dev/atibm .........................
/dev/logibm ........................
/dev/inportbm ......................
/dev/modem .........................
/dev/ttS0ttS3 .....................
/dev/cua0cua3 .....................
/dev/lp0lp2 .......................
/dev/tty1tty8 .....................
/dev/null ..........................
/dev/zero ..........................

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

;floppy
;1st IDE & CDROM drive
;1st IDE & CDROM drive partitions
;2nd IDE & CDROM drive
;2nd IDE & CDROM drive partitions
;1st SCSI & USB drive
;1st SCSI & USB drive partitions
;2nd SCSI & USB drive
;2nd SCSI & USB drive partitions
;3rd SCSI & USB drive...
;cdrom drive, linked to driver
;ATAPI IDE cdrom drive
;SCSI cdrom drive
;1st SCSI tape, auto rewinds
;1st SCSI tape, no auto rewind
;1st floppy tape, auto rewinds
;1st floppy tape, no auto rewind
;mouse
;ati graphics card mouse
;logitech mouse
;ps/2 bus mouse
;link to com port with modem
;serial port
;serial port
;parallel port
;virtual serial console
;null device
;output endless null bytes

Pg 8 of 45

May 15, 2012


Copyright 2002-2012

Other Important files


/usr/linux .........................
/etc/skel ..........................
/home/ ...........................
.profle ............................
.bashrc ............................
.exrc ..............................
.xinitrc ...........................
.fvwmrc ............................
.ctwmrc ............................
.openwin-menu ......................

;kernel source
;new user skeleton files copied to users
user personal and setup files
usr login script
usr bash config
usr vi config
usr x win startup
fvwm win mgr startup
ctwm win mgr startup
olvwm win mgr startup

X Window files
Coming soon ........................ kernel source

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 9 of 45

May 15, 2012


Copyright 2002-2012

Special Characters
|
;
&
&&
||

\
?
*
[chars]
[!chars]
<upside down

pipe connects two commands


command separator
run process in background; default stdin from /dev/null
only run command if previous command completed successfully
only run following command if previous command failed
enclose string to be taken literally
enclose string to have parameter and cmd substitution only
ignore special meaning of following character
match single character in filename
match 0 or more characters in filename
match any of chars (pair separated by a matches a range)
match any except chars
comma>
in-line command execution

Input/Output
All of these operators may be preceded by an optional file descriptor. Defaults
are shown in parentheses.
< file
> file
>> file
<&n
>&n
<&>&<< word

<<- word

use file as stdin


use file as stdout
like > but append to file if it exists
duplicate input file descriptor from n (stdin)
duplicate output file desc. from n (stdout)
close stdin
close stdout
treat line with word as EOF on input
If any of word is quoted, no additional processing is done on
input by shell. Otherwise:
parameter & command substitution occurs
escaped newlines are ignored
a \ must be used to quote \, $,
as above with leading tabs ignored

Preset Shell Parameters


Several special variables are used by the system -- you can use these, but may not be able to change
them. The special variables use uppercase names, or punctuation characters. Some variables are set by
the login process and inherited by the shell (e.g. $USER), while others are used only by the shell. These
are some of the more commonly used ones:
1
2
3
$n
$*
$@
$*
$@
$#

default input
default output
default error
use positional parameter n
all positional parameters
all positional parameters
equivalent to $1 $2 ...
equivalent to $1 $2...
number of positional parameters

There are various ways to conditionally use a variable in a command.


${datafile-default}

Substitute the value of $datafile, if it has been defined, otherwise use the string "default".
This is an easy way to allow for optional variables, and have sensible defaults if they haven't
been set. If datafile was undefined, it remains so.
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 10 of 45

May 15, 2012


Copyright 2002-2012

${datafile=default}

Similar to the above, except if datafile has not been defined, set it to the string "default".
${datafile+default}
If variable datafile has been defined, use the string "default", otherwise use null. In this case
the actual value $datafile is not used.
Substitute the value of $datafile, if it has been defined, otherwise display datafile: error
message. This is used for diagnostics when a variable should have been set and there is no
sensible default value to use.
$name
$name:
${var}
${vartext}
${var=text}
${var?text}
${var+text}

only checks if name is set


checks if name is set and non-NULL
use braces to delimit shell variable name
use var if set, else use word
use var if set, also set name to word
use var if set, otherwise print word and exit
use word if var set, otherwise use nothing
Example ${datafile?"error message"} debug message

if var is not set

Login environment
$EDITOR

$USER
$LOGNAME
$PATH

$TERM

$PAGER

$SHELL
$CDPATH
$HOME
$IFS
$MAIL
$MAILCHECK
$MAILPATH
$PS1
$PS2
$SHACCT
name=word

If set, this contains the name of the program which the user prefers to use for text file
editing. A program which needs to have the user manually edit a file might choose to
start up this program instead of some built-in default (e.g. "crontab -e". This also
determines the default command-line-editing behavior in interactive shells.
currently logged-in username
Preset to the.
The list of directories that will be searched for external commands. You can change this
in a script to make sure you get the programs you intend, and don't accidentally get
other versions which might have been installed.
The terminal type in which the shell session is currently. Usually "xterm" or "vt100".
Many programs need to know this to figure out what special character sequences to
send to achieve special effects.
If set, this contains the name of the program which the user prefers to use for text file
viewing. Usually set to "more" or "less" or something similar. Many programs which
need to present multipage information to the user will respect this setting (e.g. man).
This isn't actually used by the shell itself, but shell scripts should honour it if they need
to page output to the user.
name of default shell
search path for cd command
home directory for cd command
field separators (space, tab. newline)
name of a mail file, if any
check for mail every n seconds
fnames to check new mail (: separator;
%message)
primary prompt string ($)
secondary prompt string (>)
accounting file for user shell procedures
set name to specified word

fname

may

be

followed

by

Shell internal settings


$PWD Always set the current working directory (readonly)
$OLDPWD The previous directory (before the most recent cd command)
$?
(readonly) Set to the exit status of the last command run, so you

can test success or failure. very


command resets this so it must be saved immediately if you want to use it later.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 11 of 45

May 15, 2012


Copyright 2002-2012

$$IFS

Option flags of current shell command.


Internal Field Separators: the set of characters (normally space and tab) which are used to parse
a command line into separate arguments. This may be set by the user for special purposes, but
things get very confusing if it isn't changed back.

Process ID variables
(readonly)
Set to the process ID of the current shell - useful in making unique temporary files, e.g.
/tmp/$0.$$
$PPID (readonly) Set to the process ID of the parent process of this shell - useful for discovering how
the script was called.
$!
(readonly) Process ID of the last command started in background.
$$

ksh/bash additional features


(readonly)
Integer number of seconds since this shell was started. Can be used for timing commands.

$SECONDS
$RANDOM

Every time it is valuated, $RANDOM returns a random integer in the range 0-32k. RANDOM may be
set to "seed" the random number generator.
$LINENO (readonly)
Always evaluates to the current line number of the script being executed - useful for debugging.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 12 of 45

May 15, 2012


Copyright 2002-2012

Bash Shell Concepts


Syntax
This section provides a reference for commands built into the shell. Words with-in
<> are substitutes for unprintable characters, for example <tab>. <> may also be
used for mandatory options, files, or input.
Whitespace consists of one or more <spaces> and/or <tab> characters. In addition,
commands are terminated by any of the following characters: ; & () ^ <newline>
<space> <tab>. Boldface represents words typed exactly as they appear. Italics
represent words to be substituted for things such as filenames.
Note that if an italicized word is plural (i.e. files), multiple arguments are
permitted. A digit at the end of a word such as var1 indicates a unique name so
it can be referenced in the description. The following substitutable words are
used in this reference:
file
list
n
name
oct
pat
word

or fname: a filename, pathname, or variable of one


one or more pipelines that can be separated by ;, &, &&, || and
optionally be terminated by ; or &.
an integer
the name of a shell variable
an octal number
explained in conjunction with the case command.
a generic argument; a word. Quoting may be necessary if it contains
special characters.

Bash Startup & Kill


Configuration Files
The /etc/profile is the main startup file for all bash shells. It usually looks in
the users home directory for further instructions. The home directory should
contain one or more of the following bash startup files, which contain commands
that apply only to the current user's bash session:
$HOME/.bash_profile
$HOME/.bash_login
$HOME/profile
$HOME/.bash_rc
$HOME/.bash_logout
To understand how bash uses startup files, consider the difference between a login
shell and a non-login shell. A login shell is created when a user enters the
operating environment using a name and password account. A non-login shell is
created when an additional bash shell is launched within the user session, as you
do when you open a terminal window in X, or execute a shell program.
To starta shell, bash consults the following files in the specified order:

1.
2.
3.
4.

Login Shell
/etc/profile
$HOME/.bash_profile usually points to ->
$HOME/.bash_login
$HOME/profile

Non-Login Shell
1. $HOME/bash.rc

When you start a non-login shell, bash consults only one startup file,
Because login and non-login shells use different startup files, the shells created
may differ. Most
Linux
distributions contain an automatically
generated
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 13 of 45

May 15, 2012


Copyright 2002-2012

~./bash_profile that contains a script that automatically executes the commands


placed in ~/bash_rc. This script ensures that the login shell will be configured
like the non-login shell. To add a command that is automatically executed when any
user begins a login shell, add the command to /etc/profile. You'll need to switch
to superuser status to edit this file.
To add a command that is executed when you start a login shell in your own user
account, use ~/bash.profile for commands that must be executed before commands
contained in ~/.bash_login or ~/profile. Use ~/profile for commands that must be
executed only after the shell executes the commands found in ~/.bash.profile and
~/bash_login.
To add a command that is executed when you start a non-login shell, use ~/.bashrc.

Invoking Shell
This section shows the syntax for invocation of the shell. In order to parse the
command line properly, the arguments must be separated in a manner the shell
understands. Whitespace and/or punctuation is used. Whitespace consists of one or
more space characters, tab characters, and/or the end of line character.
Punctuation consists of the characters () ; | <> >>
The end of a command line is generally signified by entering the newline
character, usually represented by the <RETURN> key. You can also separate commands
with a semicolon (;).
The pound sign (#) character indicates the beginning of a comment to the shell.
All characters from the # to the end of the line are ignored.
sh [options] [args]
Options:
a
mark modified export variables
c cmd execute cmd, default reads cmds from file in first entry of args.
e
if non-interactive, exit if a command fails
f
disable wildcarding
h
locate and remember functions on definition instead of on execution
i
set interactive mode
k
all keyword arguments placed in environment
n
read commands without executing them
r
set restricted mode
s
read commands from stdin
t
read and execute one command, then exit
u
set error upon substituting an unset variable
v
print input lines as read
x
print commands and arguments as executed
All options except c, i, r, s can be set or unset within the shell with the set
command.

dot Command
The shell has a built-in command represented by a period . which is pronounced
dot. This command reads a file of commands without creating a sub-shell. This
means you can change our environment by interpreting a file with the dot command.
For example. if you modified your .profile you could re-interpret it without
having to log back in by entering:
. .profile
If you create various files to set up different environments for yourself, you
will probably want to set different values for PSI so you know which environment
you are in.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 14 of 45

May 15, 2012


Copyright 2002-2012

XWindow & KDE Startup


For completeness the xwin startup is briefly discussed here. The xwindow system should always be
started using the startx command which is designed to get things going. A tremendous amount of
programs and synchronization is needed for the GUI to successfully load.
startx <- shell command calls xinit command which must have things setup for it
xinit
shell command calls the following config files in order
$HOME/.Xresources <- usually exist
$HOME/.Xmodmap
<- usually exist
$HOME/.xinit
<- does not usually exist
$HOME/.Xclients
<- does not usually exist
/etc/X11/xinit/xinitrc
/etc/X11/xinit/Xclients

KDE users have a nice directory (folder) where the entire contents will be run, this is similar to MS
Windows startup folder.
$HOME/.kde/Autostart

Background Processing
Instead of running a command interactively, it can be run in the background. This
means that the command will continue to run but you will immediately get your
prompt back so that you can continue with interactive use of your terminal.
To start a command in the background. append an ampersand & to the end of the
command line. You may want to redirect the output so that it doesnt interfere
with what you are doing. For example, to find all the tiles named .core in the
/usr directory and save their names in a file, use the command...
find /u -name .core -print > temp &
Find would run and you could check to see if it was complete by using the ps
command. The file temp would grow as it received output from the command.
When a command is started in the background, the shell establishes a slightly
different environment for it. The stdin of the command is connected to /dev/null
instead of /dev/tty. This means that if the command reads any input from standard
input, it will get an end of file (EOF). Also, the interrupt and quit signals are
disabled. This means that if you interrupt an interactive program while you have a
background job running, the background job will not be affected. The hangup
signal, however, is sent to the job. If you intend to have your background job
continue even if you log out, you should precede the command with the nohup
command. This tells the shell not to send the hangup signal to the command. For
example, to run find as described above and have it ignore the hangup signal...
nohup find /u -name .core -print > temp &

Terminating a Process
The kill command can be use to terminate any process. It can send any one of many
different signals.
Dont confuse the kill process command with the computer shutdown commands.
halt
poweroff
reboot
shutdown

Environment
When you first login many defaults are setup. Two examples are your home directory
$HOME and a default search path $PATH for commands. It is possible to alter these
defaults by creating a file called .profile in your home directory and placing the
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 15 of 45

May 15, 2012


Copyright 2002-2012

commands to alter your environment in this file. Whenever you log in, the shell
interprets this file.
If you set a shell variable and want it to be exported
subsequent shells will receive its value, you must put
You may also want to define some shell functions as
used commands and place them in .profile. The following
export PATH TERM CDPATH
PATH=$HOME/bin:/bin:/usr/bin:.
CDPATH=:$HQME:/usr/local
PS1=Hi Phil >
TERM=h19
umask 027
news n

to the environment so that


it in an export statement.
abbreviations for commonly
is a sample .profile

The first line establishes three variables to be exported to the environment.


Next, the variable PATH is set to a string of directory names. Each name is
separated by a colon :. This establishes where the shell will search for commands.
In this example, it first searches sub-directory bin of your home (login)
directory, then /bin, then /usr/bin, and finally the current directory represented
by a dot.
CDPATH establishes where the cd command looks for a directory that does not
contain a leading slash. By default, it would look in your current directory. By
assigning a new value to CDPATH, the cd command would first look in the current
directory (identified by the: immediately following the =, then look in your home
directory, and finally /usr/local.
PS1 establishes your primary prompt string. This string will appear instead of the
default % as the shell prompt.
The TERM variable is used to establish the terminal type you use. If you do not
always use the same type of terminal, this line can contain a reference to the
test command to conditionally establish terminal type.
The umask command sets the file creation mask which is used to limit the access
capabilities of any files that you create. This particular value (027) establishes
the maximum access permissions as anything for owner, read and execute for group
and nothing for other.
The -n option is to print out any news items that are current and then update the
time stamp file that it uses to keep track of what you have read.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 16 of 45

May 15, 2012


Copyright 2002-2012

Built-in Shell Commands


#
.file
:
[
break [n]
cd [file]
continue [n]
echo [words]
eval [words]
exit [words]
exit [n]
export [names]
getopts
hash [r] [files]

start of comment: terminated by a newline


read and execute commands from file
null command: returns 0 exit status
see test
exit from enclosing for or while loop
change current directory to file
do next iteration of enclosing for or while
echo words (see echo in Appx. B)
evaluate words and execute result
execute words in place of shell
exit with return value n
export names to environment of commands
parse parameters and options
remember locations of files; with no files shows hash info
r forgets all remembered locations
newgrp [words]
same as exec newgrp words
pwd
print working directory name
read names
read stdin and assign to names
readonly [names] mark names read-only;
print list if no names, return n, exit with return value n
with no n return status of last command
set [options] [words]
set flags (aefhkntuvx- are valid). words set positional
parameters
set [+options] [words]
unset flags
shift [n]
rename positional parameters; $n+1 =$1... (n defaults to 1)
test
evaluate conditional expressions
times
print accumulated process times
trap [word][sigs]
execute word if signal in sigs received
no word or sigs print traps
no word reset sigs to entry defaults
word is null string ignore sigs
sigs is 0 execute word on exit from shell
type files
show how shell would interpret each file
ulimit [options] [n]
limit size to n (if no n. print current limits)
f
files created by child processes (n blocks default)
p
change pipe size to n
umask [oct]
set file creation permissions mask (see chmod)
unset [names]
unset variables or functions names
wait [n] wait for process n; if no n, wait for all children

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 17 of 45

May 15, 2012


Copyright 2002-2012

Parameters & Variables


Shell variables can be used to remember values that you intend to use later. Shell
variable names can be made up of letters a-z A-Z and the underscore _ character.
There are no data types in the shell. All shell variables are character strings.
These shell variables are built-in, named save areas that you can create by
referencing them by name. You can perform arithmetic functions on shell variables
using the expr command.

Assigning a Variable
In order to assign a string to a variable you enter what looks like an algebraic
expression. Note that there must be no spaces around the equals = character. For
example, to assign the string joe to the shell variable worker...
worker=joe
If the variable contains any special characters, including whitespace, you need to
quote it. For example...
worker=joe smith
The double quotes do not become part of the string, they just prevent the
interpretation of the special meaning of the space character in the assignment
statement.

Referencing a Variable
To reference a shell variable, precede its name by the dollar sign $ character.
For example,
meal=steak
echo $salad
;would show: steak
If you need to insert a variable reference with other characters, you can use
braces {} to delimit the variable name.
echo Eat ${meal}and milk.
;would show Eat steak and milk.
Whereas...
echo Eat $mealand milk.
would attempt to reference a shell variable $mealand that does not exist.
You can also assign command names to shell variables. For example,
cmd=ps
option=-ef
$cmd $option
;performs command ps -ef

Special Variables
There are built in variables too. For example, $PS1 is your primary shell prompt
string (usually %). Another example is $$, the process number of the current
shell. This is commonly used to create unique filenames. The following statement
would be a good way to create a unique filename. It could then be referenced by a
reference to the shell variable z. z might equel something like xyz8423.
z=xyz$$
Another special variable is $? which contains the exit status for the last command
executed. $? is set to 0 if the command executed successfully, non-0 otherwise.
Commands return a different non-zero exit status depending on the type of failure.
You can look at values of individual variables by referencing the variable in an
echo command. For example, the following command displays the values of the HOME
and TER\1 shell variables:
echo $HOME $TERM
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 18 of 45

May 15, 2012


Copyright 2002-2012

The set command displays the values of all shell variables (and shell functions).

Position Parameters
When you start a shell script you can pass parameters to it. For example, you may
need to pass a file name. These parameters are placed on the invocation line
following the command and are called positional parameters. These parameters are
referenced in the same manner as shell variables.
Description
Variable
--------------------------------------------number of positional parameters
$#
command name that invoked script
$0
1st parameter
$1
2nd parameter
$2
etc...
For example, the following command...
ls l *
Would have the following parameters,
Description
Variable
Value
------------------------------------------------------------------number of positional parameters
$#
2
command name that invoked script
$0
ls
1st parameter
$1
-l
2nd parameter
$2
*
etc...
Parameters can also be passed by name by preceding the command that invokes the
shell script with assignments to variables. For example, the following command
will invoke the shell program cool with shell variable first set to 5 and shell
variable last set to 10.
first=5 last=10 cool
These assignments do not affect the variables in the current shell. They are
merely named passed parameters, commonly called keyword parameters.

Shift Command
Shift deletes the first positional parameter $1 and shifts each subsequent
parameter one place to the left. In other words, $2 becomes $1, $3 becomes $2 and
so forth.

Set Command
The

set command allows you to do three things:


set shell options
change positional parameters
display set variables

The shell, like most UNIX commands, has options which may appear on the command
line. Unlike other commands, many of these options can be changed by the use of
the set command.
The positional parameters ($1. $2. ) can be changed using set. For example, to set
the first three positional parameters ($1, $2, and $3, to uno, dose, trace, you
would use the following set command...
set uno dose trace
After this command, the command
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 19 of 45

May 15, 2012


Copyright 2002-2012

echo $2
for example, would print
dose
Finally, set with no options prints a list of all shell variables (and functions)
that are currently defined.

Conditional Use of Variables


Sometimes it is desirable to be able to conditionally use values of a shell
variable. For example, if a variable is missing it may be desirable to insert a
default value. This can be done by an extension of the variable reference notation
using braces. The expression ${var} is a reference to the value of the shell
variable var. The following additional forms are available. Note that without the
: in the expression, the variable is only checked to see if it is set; whether or
not the variable is non-null is not considered.
${var:-word}
${var:=word}

${var:?word}

${var:+word}

Substitute value of var if set and non-null; otherwise


substitute word.
If variable is not set or is null, set it to word. Then use
value of var. Note that this cannot be used to assign values to
positional parameters.
Use value of var if set and non-null. Otherwise, print word and
exit from the shell, If word is omitted, parameter null or not
set is printed.
If var is set and non-null, word is substituted, Otherwise
nothing is substituted.

The following version of append will append to the file /tmp/app.data if you do
not specify a file name on the invocation line. In other words, if $1 is not set,
the file /tmp/app.data is used.
# append version 1
echo control-D to terminate
cat >> ${l:-/tmp/app.data}
The following version of append complains when it is invoked without a filename on
the command line.
# append version 2
echo controlD to terminate:
cat >>${l:?filename required)
This version prints out the message append: 1:
filename required if you enter the command with no parameters. Unfortunately. the
error message is printed out after the information message from echo. Version 4
gets around this problem by assigning the parameter to a shell variable named fn
which Is then used in the cat command if the substitute doesnt tail.
# append version 3
fn=${1:?filename required}
echo controlD to terminate:
cat >>$fn

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 20 of 45

May 15, 2012


Copyright 2002-2012

The Environment
The environment consists of pairs of names and values. When a shell is started
(either from logging into the shell or from starting a sub-shell) the shell scans
the environment and creates a variable with the related value for each pair found.

Exported variables
When a one program invokes another program, the child program receives all of the
exported environment variables of the parent.
By default, environment variables are not exported.
Shell variables can be bound to the environment by use of the export command. If a
variable is named in an export statement, any future shells will inherit this new
variable and its associated value. Note, however, that you cannot pass information
back to a calling shell through this mechanism.

Common shell variables


Most shells have the following set:
variable
DISPLAY
EDITOR
HOME
PAGER
PWD
SHELL
TERM
USER

description
tells X11 on which display to open windows
default text editor; usually emacs or vim
path to user's home directory; same as ~
default page-scroller to use; usually less
current directory; same as output of pwd
path to the current shell
current terminal type
account name of current user

The statement export with no arguments will display all variables that are
currently being exported. One of these, $HOME, is the pathname of your login
directory, is initially extracted at login time from your password file entry.
For example, the new version of append looks in the environment for a variable
named afile to determine what file to append to if none is specified on the
command line.
# append version 4
fn=${1:${afile:?filename reqd}}
echo appending to $fn
echo control-D to terminate:
cat >> $fn

Regular Expressions
Note: multiple res may be concatenated
c
the character c
\c
character c (for special characters)
.
any character except a newline
[str]
any one character in str ([a-b] means all in range a thru b)
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 21 of 45

May 15, 2012


Copyright 2002-2012

[^str]
re*
re\{m,n\}
\(re\)
\n
^
$
<null>
&
%

any character except newline or one from str


0 or more occurrences of 1 character re
m thru n occurrences of 1 character re
group re for later reference
nth re in\(..\)
beginning of line (not in expr)
end of line
default re last re encountered
put matched string in replacement
same as last replacement

Wild Cards
Wild Strings
The asterisk * matches any number of characters. In other words, 0 or more of any
characters. For example, if you wanted a long list of the characteristics of all
files in the current directory whose names contained the string test. you could
use the following command:
ls l *sample*
The first * means that there can be 0 or more characters before the literal string
sample and the second * means there can be 0 or more after the string.
Therefore, files with names like sample01, xsample, and ysample33.db will all be
listed.

Wild Character
Single character made with the question mark ?. For example, a long listing of all
files whose names begin with z and are exactly three characters long would be
printed by the command...
ls l z??

Match Lists
A third kind of wildcard is a match list. The syntax is [list] here list is the
list of characters to be matched. For example, the following echo command will
show all the file names whose first letter is a, second letter is b or c, and
third letter is d.
ls l a[bc]d
A range can be specified by using a dash, so the following would match files whose
names begin with w, second letter is h,I,j,k, third n, and forth x,y, or z.
ls l w[h-knx-z]
The three kinds of wildcards can be combined to perform matches. For example. the
following command would echo all file names whose third letter is x,y, or z.
echo ??[x-z]
The exclamation ! character can be used to reverse a comparison. It must appear
as the first character inside the brackets. For example, the following command
would produce a long list of all files whose names do not start with a lower case
letter:
ls l [!a-z]*
Testing Wildcards
The use of wildcards can be dangerous. It is very easy to inadvertently remove the
wrong files by using the rm command in conjunction with wildcards. If you are not
sure what files will be effected, you can use the echo command to test the
pattern.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 22 of 45

May 15, 2012


Copyright 2002-2012

Quoting
It is necessary to quote strings in order to prevent the shell from treating
various characters as special. You may need to quote a string to prevent the shell
from erroneously interpreting the special meaning of characters such as * or?.

Single Quotes
The apostrophe or single quote is used to indicate that all characters in the
enclosed string are to be taken literally. For example,
echo *[xyz]??
would echo the string *[xvz]?? rather than any matched file names.

Double Quotes
The double quote tells the shell to interpret only the special characters,
dollar sign $, and the backquote `. The special meaning of other characters is not
interpreted within double quotes.

Single Character Escape


The special meaning of a single character can be escaped by preceding it with a
backslash \.
echo can\t
will print cant. Double quotes could have been used here but there will be times
when the escape character will be the only answer.
echo dont
The backslash also prevents the interpretation of the newline character. This
means that you can continue a command line by entering a \ before the new line and
then continue typing on the next line.
See the Difference?
The following statements illustrate the difference between quotes...
# assign * asterisk to variable xy.
echo $xy
;prints $xy
echo $xy
;prints *
echo $xy
;prints <all files>
echo \$xy
;prints $xy
1) The first example prints $xy because the $ is protected from interpretation
of its special meaning.
2) In the second line, echo prints *, the value of the shell variable me is an
asterisk.
3) The third echo lists all files. The shell first substitutes $xy to its
value the re-scans the line. It is as if you had typed echo *.
4) And the last statement uses the escape character \ to force the $ to be
interpreted as a regular character. Therefore, $xy is shown.
Backquote
The backquote (grave accent) ` tells the shell to treat the enclosed string as a
command. The command is executed and the resulting string (output from the
command) is substituted in the command line. One use is to assign values to shell
variables.
echo `date`

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 23 of 45

May 15, 2012


Copyright 2002-2012

I/O Redirection
Note that in redirection, no filename expansion takes place. Therefore,
occurrence of shell meta characters such as * or ? is treated literally.

any

There are three file descriptors which are normally connected to your terminal.
Redirection is the process of changing the connections from your terminal to a
file or a program. The three file descriptors are...
Name
descriptor
description
--------------------------------------------------------------------stdin
0
standard, default input
stdout
1
standard output, default output
stderr
2
standard error, default error messages

Output Redirection
Both stdout and stderr can be redirected to a file. To create (or truncate) a file
and send standard output to a file, the > operator is used. For example. to send
the standard output of the date command to file named sample, use the following
command:
date > sample
Note: Using > causes a file with that name to be created and input sent to it. If
the file already exists, it will be erased and only the new information will be
added. To create a new file if none exists and add to the end of an existing file,
use >>. Therefore, running date > twice will erase the first date.
date > sample
date > sample
Now, running date >> will continue to add each date line to the end of the file.
The >> will save both commands output.
date >> sample
date >> sample
Note that since both the > and >> operators are punctuation, you do not need to
include spaces around them. The following three command lines are interpreted
identically:
PS ef >stats
PS ef > stats
ps ef>stats
To redirect standard error you use the same operators but precede them by the file
descriptor number (in this case 2) of standard error. For example, to run cc on
the file test.c and send errors to a file, you could use the following command...
cc test.c 2 > error.log
Because the redirection commands are interpreted by the shell, they are never seen
by the actual program that invokes it. Thus, where the redirection appears in the
command line is unimportant. The following command would be equivalent to the one
above...
cc 2 > error.log test.c
The operator >& can be used to duplicate an existing file descriptor. This
basically means that the two output streams are merged. For example, if you wanted
to run the pr command on files datal, data2, and data3 and merge the output and
error outputs into file all.log, you could use the following command...
pr data[123] > all.log 2>&l

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 24 of 45

May 15, 2012


Copyright 2002-2012

This command says to redirect standard output to the file all.log and create file
descriptor 2 by making a copy of file descriptor 1, the descriptor number for
stdout.
It you want to redirect stdout or stderr for multiple commands, you can use the
exec built-in function with redirection. The redirection will remain in effect
until another exec command with redirection is given. Note that /dev/tty is a
filename for your terminal. You can always use it to send output directly to the
terminal or redirect output back to the terminal.
For example, the following set of commands on your current stdout, sends the
output of the ps and who commands to the file myout, to the terminal, sends the
output of date to my out and finally resets the standard output back to the
terminal:
echo 1,2,3 test
;prints stdout message 1,2,3 test
exec > myout
;change stdout to a file
ps
;commands now sent to file myout
who
echo temporary redirect > /dev/tty
;echo temporary redirect
date
;date is sent to file myout
exec >/dev/tty
;stdout set back to terminal
echo back to normal
;echo now goes to terminal
The operator >&- will close standard output. Like all the other operators in this
section, preceding it with a file descriptor number (i.e. 2 for stderr) causes it
to refer to a different file descriptor.
If you are running a command and want to discard its output you can redirect it to
the special file /dev/null. This is bit heaven , the output is sent, never to be
seen again.

Input Redirection
Many commands will read input from either a file named on the command line or from
stdin. The wc command is a good example. The two command lines below do basically
the same thing. The difference is that in the first line, the wc program is
actually opening and reading the file /etc/passwd, whereas the second example has
the shell redirect stdout input from the file /etc/passwd before the wc program is
executed.
wc /etc/passwd
wc </etc/passwd
To specify a command is to read input from a file, use the < operator. For
example, to specify that the command batch (run commands as load permits) should
read its input from the file nightly, enter...
batch < nightly

Here Documents
A second form of input redirection, called a here document, tells the shell that
input follows the current line. This is used in shell scripts where a command
needs to read input up to an end of file (EOF). You can embed your input in the
command file using this capability.
Here documents use the << operator and a word that indicates how to tell when the
end of the input for the command is reached. In the following example, the cat
command will display the message Hello there! How are you?, on two lines of
stdout. The words mama cries is used as the indicator. It is defined by its
appearance after the <<operator and when it is encountered on a line by itself
(after How are you?) the end-of-file status is returned to the cat command.
cat <<mama cries
Hello there!

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 25 of 45

May 15, 2012


Copyright 2002-2012

How are you?


mama cries
Note that both parameter and command substitution are performed on the EOF
indicator word. Once performed, a literal comparison is performed to find the
matching word.
Normally, parameter and command substitution are performed on the input, so it is
necessary to use the backslash \ to escape the special meaning of \, $, and . If,
however, you quote any part of the EOF indicator word, shell processing of the
input is turned off. The following script sends the message exactly as it appears
with no parameter substitution, to user joe using the mail command.
mail joe <<EndEOF
Hi joe.
You owe me $4.50 for lunch.
Remember our date?
EndEOF
Without the quotes around EndEOF, the $ would be treated as a reference to a shell
parameter and the date would cause the date command to be executed and the
result would replace the string date.
A hyphen - can optionally be appended to the << operator. If it is, leading tab
characters are stripped from both the EOF indicator word and the subsequent lines
of shell input.

Pipes Connecting Commands


A command that reads stdin and writes stdout is called a filter. Filters can be
connected to other commands using the pipe its symbal is |. Note that the command
doesnt have to use both stdin and stdout.
In redirection, you are using a file as either a source (stdin) or an output
(stdout and stderr).
Connections are not limited to two programs. You could use the following sequence
to list file names in 5 columns and page them on the screen:
ls | pr 5 | pg
Also, pipes and redirection can be combined in the same command line. For example,
to perform the same function as above but send the output to a file...
ls | pr 5 > tmp.file

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 26 of 45

May 15, 2012


Copyright 2002-2012

Unix Commands
Some commands, although not part of the shell, are necessary to do useful work
with the shell. The main difference to the user is that these commands will take
slightly longer to execute because they must be loaded from the file system. In
fact, some commands started out as separate programs and were later added to the
shell to speed execution, test is an example of such a command.

Search Order
When you enter a command, the shell first checks to see if it is one of the
commands built into the shell. Second, it checks to see if it is a shell function.
If the command isnt a built-in or a shell function, the shell searches the
directories listed in sour PATH shell variable in the order listed to try to
locate the command. This means that shell functions override commands that are
stored in files but built-in commands always override shell functions.
The type command, a shell built-in command, can be used to determine where the
shell will locate a command. Just enter type followed by the command
name and the shell will display where it located the command.

Chmod Command
chmod <mode> <files>
;change permissions of a file or directory
mode can be numeric or symbolic. The symbolic case consists of the form
[agou][+=][rstwx] where:
a all default (group, other, user)
g group access permissions
o other access permissions
u user access permissions
+ add the permission to status of files
remove the permission from status of files
= set the permission of files to specified value
r read permission
s set owner-ID or group-ID on execution save text mode
w write permission
x execute permission Multiple symbolic modes separated by commas
The numeric case is formed from the following columns:
4000
set user ID on execution
2000
set group ID on execution
1000
set sticky bit (leave program in memory as long as possible)
0X00
owner permission, X added 04 (read), 02 (write), 01 (execute)
00X0
group permission, X added 04 (read), 02 (write), 01 (execute)
000X
other permission, X added 04 (read), 02 (write), 01 (execute)
example:
0744
owner=read, write, execute
group&other=read only
0660
owner and group may read or write

Echo
echo [message]
;echo message to stdout
Note: special escape conventions (place string in quotes)
\b
backspace
\c
print line without newline form feed
\n
newline
\n
octal val (8 bits), n must start w/ 0 carriage ret
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 27 of 45

May 15, 2012


Copyright 2002-2012

\t
\\

tab
backslash

Expr
expr args
;evaluate expression
Arguments
must
be
separated
by
blanks,
are
evaluated
as
follows.
A \ indicates that the following character must be escaped:
expr1 \| expr2
returns expr1 if neither null nor 0; else expr2
expr1 \& expr2
returns expr1 if neither expr is null or 0; 0 otherwise
expr1 cmpop expr2
returns result of integer compare if both args are ints;
otherwise, returns result of lexical compare
cmpop is:
=
equal \> greater than
>=
greater or equal
\<
less than
\<=
less or equal
!=
not equal
expr1 mathop expr2 returns result of integer arithmetic on expr
mathop is:
+
addition
subtraction
*
multiplication
/
division
%
remainder
expr: re
compare expr with regular expression re search always
anchored to beginning of line (^ not special)
returns number of chars matched (0 = fail) \(...\) can be
used to return portion of expr
The eval command is used to force the shell to rescan a command line and execute
the result. If you have constructed a command line from shell variables and passed
parameters. it may be necessary to rescan it in order to get the intended meaning.
Another way of understanding the action of eval is to consider what the shell
would do with your command line after substitution took place. For example, if one
of the substituted values had the $ character in it, the shell would interpret
this as a reference to a shell variable and it would attempt a substitution. This
capability of eval allows you the equivalent of indirect addressing. using the
contents of a variable as the address of data rather than the actual data.
In the following example, the script etest, the string $data is assigned to shell
variable ptr. The single quotes are necessary to prevent the interpretation of the
$. Next, the string this is it is assigned to shell variable data. The first two
echo commands display the values of ptr and data to verify the assertions above.
Finally the eval command line evaluates all its arguments and prints the result,
This means that $ptr is interpreted and is replaced by its value $data. Then the
echo command embedded in the eval command is executed. After the rescan, this echo
becomes echo $data and the printed result is this is
it.
* etest
ptr= $data data=:his is it
echo ptr is .$ptr.
echo data is .$data.
eval echo $ptr
Unix shell variables contain character strings. The shell itself has no concept of
arithmetic operations. For example, the following command sequence
x=5
x=$x+$x
echo $x
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 28 of 45

May 15, 2012


Copyright 2002-2012

will print out 5+5, not the value 10 that you might have expected. This is where
the expr command comes in. expr treats its arguments as operators and operands and
performs arithmetic and comparison operations on them. There are two catches:
integer (not floating point) arithmetic is performed and each operator and each
operand must be passed to expr as a separate argument.
Here are some simple examples:
expr 5 + 3
prints out 8.
expr 12 / 3 + 9
prints out 13, unfortunately,
expr 5 * 3
prints out expr: syntax error. This is because the shell expands the * into a list
of filenames in your current directory. Escaping the *, either with a backslash or
quotes, will solve the problem as in any of the following:
expr5\*3
#use\
expr 5 '*/ 3
# or expr 5 "*" 3 # or
Note, however, that you cannot just put quotes around the complete expression as
in 5 * 3. This would cause the shell to pass the whole expression as one
argument. expr would just print out the string rather than evaluate it.
Expanding on this idea, shell variables can be inserted into expressions and we
have shell arithmetic. For example,
expr $count + 1
would print out the value of shell variable count plus 1. By using backquotes we
can get the shell to
execute the expr command and assign the result to a shell variable. Thus, we can
increment shell variables.
count=expr $count + 1
Using this capability it is easy to write a primitive version of the pg utility.
In this version, standard input is read and output pauses every 20 lines. It is
restarted by entering a return.
# dumb_pg
count=0
while line
do
count=expr $count + 1
if [ $count eq 20 ] ; then
read stuff </dev/tty
count=0
fi
done
The shell variable count is initialized to 0. Then a while loop is executed. The
loop continues until the command line returns a false exit status, line is a
command that copies a line from standard input to standard output.
line returns true unless an end of file is encountered. Therefore, the while loop
will execute as long as there is input to read. Shell variable count is
incremented and then tested to see if it is equal to 20. If it is, a read is
executed that will cause execution of the script to stop until something is
entered. The argument to read (stuff) is required. Whatever is entered is assigned
to stuff but it is never referenced. count is also reset to 0 and the sequence
starts over.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 29 of 45

May 15, 2012


Copyright 2002-2012

A complete list of all the operators for expr as well as a definition of regular
expressions is given in Appendix B. One interesting operator for string operations
is the colon (:). It is used to match characters in the first operand against a
regular expression specified in the second operand. The only difference between
the regular expressions used by ed and those used by expr is that expr anchors all
searches to the beginning of the string. This means that is not a special
character with expr.
The : operator of expr normally returns the number of characters successfully
matched; 0 if no characters match. Using this return value and the general match
anything pattern. .*. the following shell script will return the number of
characters in a string:
#len length of a string
expr $1, : . *
One final note. expr receives arguments after they have been processed by the
shell. Therefore, if a shell variable contains a string that looks like one of
exprs operators. expr ill become confused. For example, the following would
confuse expr:
x=
expr $x = =
This is because, after shell substitution, the command to expr looks like
expr = = =
The solution is to precede the operands with some character that does not have a
special meaning. For example.
expr Z$x = Z=
will form the same comparison as the earlier example but will not fail because of
the special meaning of the = character.

False
false

;Return unsuccessful Exit Status

True
true

;Return successful Exit Status

Go Function
The following shell function is a replacement for the cd command. By using go
instead of cd your prompt string will be set to the last part of the pathname of
your current directory.
go()
cd $1
PS1= pwd
PS1=basenarne $PS1` >
You use it just like cd. For example. to change to directory /usr/me/junk von
enter
go /usr/me/ junk
Your prompt would then be set to junk>.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 30 of 45

May 15, 2012


Copyright 2002-2012

redo
The following shell function will repeat the specified command forever at the
specified interval. Useful for running, a process status every 10 minutes. It
clears the screen before each execution.
redo () {
clscr=tput clear
wtime=$l
save wait time
shift
# shift out wait time
while true
# do forever
do
echo $clscr
clear screen
$*
# execute command
sleep $wtime
done
The parameters to redo are the W ait time and the command line to execute
repeatedly. For example. to run a ps -ef command every 10 minutes (6(X) seconds),
enter:
redo 600 ps -ef
Assigning the output from the tput command to clscr means that it only needs to be
executed once. The wait time is assigned to the shell variable wtime and then it
is shifted out of the command line. Finally. a while loop is executed which echoes
the clear string. executes the command line (referenced by $* and sleeps for the
specified time.

Kill
kill [option] pids
option:

signo decimal number of signal sent (15 default)


1 hangup
2 interrupt
3 quit
4 illegal instruction
5 trace trap
6 IOT instruction
7 EMT instruction
8 floating point exception
9 kill
10 bus error
11 segmentation violation
12 bad system call argument
13 write on unread pipe
14 alarm clock
15 software termination signal
16 user defined signal 1
17 user defined signal 2

Numb
er

1
2
3
4
5
6

;Terminate or send Signal to processes

Standard BSD signals


.
Description
Default

Symbolic
Name

SIGHUP
SIGINT
SIGQUIT
SIGILL
SIGTRAP
SIGIOT

Hangup
Interrupt
Quit
Illegal instruction
Trace trap
lOT trap

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Terminate
Terminate
Terminate
Terminate
Terminate
Terminate
Pg 31 of 45

Can
Can Dump
Be
Be
s
Caug Blocke Core
ht
d
Yes
Yes
No
Yes
Yes
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
May 15, 2012
Copyright 2002-2012

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

SIGEMT
SIGFPE
SIGKILL
SIGBCS
SIGSEGV
SIGSYS
SIGPIPE
SIGALRM
SIGTERM
SIGURG
SIGSTOP
SIGTSTP
SIGCONT
SIGCHLD
SIGTTIN

22

SIGTTOU

23
24
25
26

SIGIO
SIGXCPU
SIGXFSZ
SIGVTALR
M
SIGPROF
SIG WINCH
SIGLOST
SIGUSR1
SIGUSR2

27
28
29
30
31
pids

EMT trap
Arithmetic exception
Kill
Bus error
Segmentation violation
Bad argument to system call
Write on a pipe with no reader
Alarm clock
Software termination signal
Urgent socket condition
Stop
Keyboard stop signal
Continue after stop
Child status has changed
Background read from control
terminal
Background write to control
terminal
10 is possible on a descriptor
CPU time limit exceeded
File size limit exceeded
Virtual time alarm

Terminate
Terminate
Terminate
Terminate
Terminate
Terminate
Terminate
Terminate
Terminate
Ignore
Stop
Stop
Ignore
Ignore
Stop

Yes
Yes
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
Yes
Yes
Yes

Yes
Yes
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
Yes
No
Yes
Yes

Yes
Yes
No
Yes
Yes
Yes
No
No
No
No
No
No
No
No
No

Stop

Yes

Yes

No

Ignore
Terminate
Terminate
Terminate

Yes
Yes
Yes
Yes

Yes
Yes
Yes
Yes

No
No
No
No

Profiling timer alarm


Window changed
Resource lost
First user defined signal
Second user defined signal

Terminate
Ignore
Terminate
Terminate
Terminate

Yes
Yes
Yes
Yes
Yes

Yes
Yes
Yes
Yes
Yes

No
No
Yes
No
No

process id number(s) to receive the signal


0 implies all processes resulting from current login)

Shutdown
shutdown <mode> <time> [message]
Example -h = halt, now = time
Shutdown h now

Other options inclued r = reboot, -k = kidding (sends kill message but does not shutdown)

Set Command
srcfile=dataset1

Creates (if it didn't exist) a variable named "srcfile" and sets it to the value "dataset1". If the
variable already existed, it is overwritten. Variables are treated as text strings, unless the
context implies a numeric interpretation. You can make a variable always be treated as a
number. Note there must be no spaces around the "=".
It is possible to export a variable just for the duration of a single command using the syntax:
var=value command args
set
Display all
unset srcfile

the variables currently set in the shell

Remove the variable "srcfile"


srcfile=
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 32 of 45

May 15, 2012


Copyright 2002-2012

Give the variable a null value, (not the same as removing it).
export srcfile

Added srcfile to the list of variables which will be made available to external program through
the environment. If you don't do this, the variable is local to this shell instance.
export

List all the variables currently being exported - this is the environment which will be passed to
external programs.
To set a variable option, enter the set command followed by the desired option or
options. For example. to set x
and v. enter:
set -xv
Unsetting options is similar, except on replace the -with a +. For example, to
unset the u and y options. enter:
set +uv

Because the set command can be embedded in a script as well as entered from the
command line. various options can be turned on and off for part of a script. For
example, if you are having trouble with a shell script, you could add a set -x
command after all of the commands in the script that you know execute properly.
Then you would only get debugging information for the part of the script w here
the error was occurring.
The -f option of set can be used to turn off filename expansion (wildcard
interpretation). The following example implements a simple calculator as a shell
script using expr:
# math
set -f
echo math: enter: num op num"
echo "op is --, , *, / or %
echo math > \c
read numi op num2
echo $numl $op $num2 = \c
expr $num1 $op $num2
It is saved in a file called math. To use. you enter math and it prompts for the
operator and two operands. It then uses expr to compute and print the result.
If we entered this script without the set -f command. it would work fine until we
entered an expression involving multiplication (the * operator. The * would be
expanded into the list of filenames in the current directory. By setting -f this
expansion is turned off and the script ill work for all operators that are
accepted by expr.
If you need to determine which flags are set, they are saved in the - shell
variable. The command:
echo $will display all the set flags.

Test
test expression
Expressions:
b file
c file
d file
f file

;evaluate expression

true
true
true
true

if
if
if
if

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

file
file
file
file

exists
exists
exists
exists

and
and
and
and

is
is
is
is

a
a
a
a

Pg 33 of 45

block special file


character special file
directory
regular file
May 15, 2012
Copyright 2002-2012

g file
k file
n string
n1 eq n2
n1 ge n2
n1 gt n2
n1 le n2
n1 lt n2
n1 ne n2
p file
r file
s file
string
s1 = s2
s1 != s2
t [fd]
u file
w file
x file
z string

true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true
true

if
if
if
if
if
if
if
if
if
if
if
if
if
if
if
if
if
if
if
if

file exists and has set-GID bit set


file exists and has sticky bit set
string is of non-zero length
integers n1 and n2equal
integer n1 >= and n2
integer n1 > n2
integer n1 <= n2
integer n1 < n2
integers n1 and n2 unequal
file exists and is a named pipe
file exists and is readable
file exists and has a non-zero size
string is not the null string
strings S1 and s2are the same
strings S1 and s2are not the same
descriptor fd associated with terminal
file exists and has set-UID bit set
file exists and is writeable
file exists and is executable
string has zero length

Expressions may be joined by


!
logical negation
a
logical and
o
logical or
\(expr\)
grouping parentheses (escaped from shell

Trap Command
The trap command is used to capture control in case of an error or other program
interruption. In UNIX terminology this is called catching a signal. These signal
numbers are described under the kill command in Appendix B. One exception, the
kill signal (number 9), cannot be captured.
Syntactically, you specify the command(s) to be executed if the signal occurs as
the first argument to trap followed by a list of signal numbers. For
the shell variable loops. Then it uses a while loop to invoke the remaining
command. After each iteration. expr is used to decrement the loop count. Once the
count reaches 0. the loop is terminated.

Conditional Statements
If Statement
The if statement can be used to evaluate the result of the execution of a command
and take an action based on the truth value of this result. The most basic form of
the if statement is as follows:
if cmdl
then
cmd2
fi
In this case, cmd1 is executed, then if the result was true, cmd2 is executed.
Otherwise, control is transferred to the statement following the fi. For example,
if we wanted to add a message to indicate successful completion to the append
script from the Parameters & Variables section, we could do the following...
# append version 5
fn=${1:-${afile:?filename reqd}}
echo appending to $fn
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 34 of 45

May 15, 2012


Copyright 2002-2012

echo controlD to terminate:


if cat >>$fn ; then
echo append successful"
fi
The message append successful is printed if the cat command returns a true exit
status (in other words, worked successfully).
A more sophisticated example might take advantage of
the command test. read reads one line from standard
input to variables on the read command line. One
variable. with any leftover words assigned to the last

the shell command read and


input and assigns words of
word is assigned for each
variable.

The test command evaluates an expression and returns the value true if the
expression is true, false otherwise. These expressions are made by combining
operators, commonly called primitives, with filename and string operands. See the
reference section for a complete list of primitives. Note that there are two ways
to write a test. The following two lines both test to see if the two shell
variables, $1 and $stuff. are identical. They are equivalent and can be used
interchangeably.
test $1 = $stuff
[ $1 = $stuff ]
In the following example, the script erm first echoes the passed parameters, asks
for confirmation and then deletes all the files specified in the passed parameters
if the answer to the confirmation request is y.
# erm
echo About to remove:
echo $*
echo Enter y to remove
read answer
if test $answer = y ; then
rm $*
fi
The quotes around $answer act as a placeholder. This handles the case where the
user entered no answer, but instead hit return in response to the read command.
The indenting is cosmetic. An alternative to write this. script is:
# erm version 2
echo About to remove: echo $*
echo Enter y to remove
read answer
if [ $answer = y ]
then rm $*
fi
The differences are the use of [] instead of test and the introduction of then on
the same line as the test.
Using conditionals and test can further modify append to verify that the specified
file even exists before attempt to append to it. The -f primitive of test can be
used to test for the existence of a regular file. The exclamation point (!) is
used to negate the truth value of an expression. The exit shell command is used to
terminate a script. It returns its argument to the script where zero is true and
non-zero is false.
append version 6
fn~${1:-${afile:?filename reqd}}
if test ! f $fn ; then
echo $fn doesnt exist
exit 1
fi
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 35 of 45

May 15, 2012


Copyright 2002-2012

echo "appending to $fn


echo "control-D to terminate:
if cat >> $fn
then
echo append successful
fi
The full form of the if statement is as follows (where [] are used to indicate
optional expressions):
if cmdl
then
cmd2
[elif cmd3 then cmd4]...
[else cmdsn]
fi
Using this form of the if statement, we can add a message in response to a request
to not continue with the remove.
# erm version 3
echo About to remove:
echo $*
echo Enter y to remove files
read answer
if [ $answer = y ] ; then
rm $*
else
echo remove aborted
fi

While Statement
The while command repeatedly executes a command and each time the execution
returns a true exit status a second set of commands is executed. The while command
takes the following form:
while cmd1
do
cmd2
done
As long as the execution of cmd1 returns a true exit status, cmd2 is repeatedly
executed. A simple way to use while is creating a loop that will be executed
forever. In the following example, a process status (ps) command is executed every
60 seconds. The tput command clears the screen before each execution of ps.
while sleep 60
do
tput clear
ps -a
done
Another example uses while and the built-in shell shift command to print the
arguments to a command. The following script prints the arguments passed to it.
# printarg
echo $0 called with $# arguments
echo They are:
while [ $# gt 0 ]
do
echo
$1
shift
done

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 36 of 45

May 15, 2012


Copyright 2002-2012

Until Command
The until command has exactly the same syntax as the while command. The only
difference is that an until loop is executed as long as the command returns false
rather than true.
The following script will check for the existence of a specified file. It checks
every 60 seconds and once the file exists, it is displayed.
# exprint
until test f $1
do
sleep 60
done
cat $1

For Command
The for command can be used to sequentially process a set of strings. It
successively assigns a string to a shell variable from a specified list of strings
and then performs a specified loop. Here is the basic syntax:
for name [in strings ]
do
cmds
done
Note that the brackets are not part of the command. They indicate that the
contained information is optional. If in strings is not specified, all arguments
to the shell file ($*) are used.
The following shell script takes a list of filenames as arguments and displays
whether they are readable. It uses the -r primitive of test.
# canread
for x ; do
if [ r $x ] ; then
echo $x is readable,
else
echo $x cannot be read
fi
done
It is possible to generate a set of strings for the for command by executing
another UNIX command. For example. the following script performs the same
readability test but it is performed on files whose names are in a file specified
on the command line. The cat command is used to read this file and supply its
contents to the for command.
# canread2
for x in cat $1
do
if test r $x ; then
echo $x is readable
else
echo $x cannot be read
fi
done

Case Command
The case command selects a set of commands to execute by matching cases to a
control string. If the control string matches a case pattern, the associated
commands are executed.
The form of the case statement is:
case string in
pat!) eomniands1:;
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 37 of 45

May 15, 2012


Copyright 2002-2012

pat2) (omrnands2;;
esac
string is successively compared to pat1, pat2,... until a match is found. When
there is a match, all commands up to a double semicolon (;;) are executed.
The shell wildcards * and ? can be used in patterns as well as the [] notation for
selection. Also, the I (or) symbol can be used to indicate that if either
expression matches, it is considered a match.
For example, the following commands print a greeting based on the current hour
returned by the date command.
hour=date +%H`
case $hour in
0?|1[0l]) echo Good Morning;;
l[27])
echo "Good Afternoon;;
18)
echo Dinner Time!;;
*)
echo Good Evening;;
esac
Note that the * pattern is used as the alternative if all else fails. As it will
match anything. If used, it must appear as the last choice.

Shell Functions
Starting with System 5. Release 2. shell functions have been available. A shell
function is an alternative to placing shell commands in a file to make a shell
script.

Functions vs. Scripts


The biggest advantage of shell functions over shell scripts is fast execution. As
they are saved in the data area of the current shell, a file sy stem search is not
required in order to locate and execute them.
On the negative side, shell functions are always local to the shell that defined
them. This means that each shell you create must redefine the functions if it
needs access to them.
Another way shell functions differ from shell scripts is their access to shell
variables. Because a shell function is executed by the current shell whereas a
shell script is executed by a sub-shell, functions have access to all the
variables of the current shell. This means, for example, that a cd command in a
shell function will change your current directory and it will stay changed after
the function has completed execution. A cd command in a shell script is only
effective for the execution life of the script.

Defining a Function
The general syntax to define a function is:
func( ){ list;}
where func is the function name and list is a list of shell commands that form the
function definition. Note that a space is required following the opening brace in
the definition. If the closing brace does not appear on the same line as the last
command in list, the semicolon is optional.
The following function looks for matching lines in a file named .fone in your home
directory; and then in /etc/fone. Note that the word $* is a special variable
that means substitute all parameters here but put quotes around the outside of
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 38 of 45

May 15, 2012


Copyright 2002-2012

them so they are treated as a single argument to the grep command. This means that
the function will pass multiple words to grep in a single argument which makes
lookups of, for example. John Smith possible.
phone()
grep
$* $HOME/.fone /etc/fone
The function phone was entered on three lines. Once the first line was entered
(which must include the opening brace ({)) the shell prompted with its secondary
prompt string (> by default) until a matching close brace was encountered. You
could have also entered the function on one line, but if you did. you need to
remember the space following the { and the required before the }.

Where to Define Functions


If you enter a function definition from the command line, it will exist for the
life of the current shell. Generally. however, it is desirable to either always
have functions defined or have a set of canned functions that you can define
easily when desired.
You can enter functions that you want always defined into your .profile. This
means that whenever you log on. the definitions will be interpreted.
If you have various sets of functions that may be useful i/o a particular project.
you can include them in files. i.e.. put all related function definitions in one
file. Then you can use the dot (. command to activate the definitions when
desired. You may also want to modify your prompt string to indicate that a
particular set of functions has been activated.
For example, if you are working on a book, you could put all the functions and
other shell commands to set up your environment the way you want it for working on
the book in one file. You can then activate these definitions using the dot
command. Here is an example:
# .book - Setup book envirionment
cd /usr/me/Book
PS1=Book >
draft() {
nroff -mm bhdr $* | lp
}
final() {
troff -Tps mm bhdr $* | devps > /tmp/BK$$
echo cutput in /tmp/BK$$
}

Status of Definitions
If you define shell functions you may not remember which ones are defined or what
the definition contains. You can use the set command to find out. A set command
with no arguments displays all currently defined shell functions as well as shell
variables,
If you find that you want to remove a definition you can use the unset command.
For example. to remove the definition of the shell function draft from the current
shell you would enter:
unset draft

Search Order
When you enter a command, the shell first checks to see if it is one of the
commands built into the shell. Second, it checks to see if it is a shell function.
If the command isnt a built-in or a shell function, the shell searches the
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 39 of 45

May 15, 2012


Copyright 2002-2012

directories listed in sour PATH shell variable in the order listed to try to
locate the command. This means that shell functions override commands that are
stored in files but built-in commands always override shell functions.
The type command, a shell built-in command, can be used to determine where the
shell will locate a command. Just enter type followed by the command
name and the shell will display where it located the command.

Command Line Shell Invoking


In the previous sections the shell has been treated as either the command
interpreter waiting for you command when you log on or as an interpreter of
scripts that is invoked by entering the name of the script.
Actually, the
found in the
current shell
value of the
/bin/sh.

shell is just another UNIX command. Its name is sh and it can be


/bin directory. When you enter the name of a shell script, your
invokes a new shell (sub-shell) to interpret it. Your shell uses the
variable SHELL as the pathname for the new shell. The default is

Like most UNIX commands, there are various options that can be placed on the
invocation line for the shell. Many of these options can be changed within your
current shell by use of the set command. In this section you will learn about the
invocation options and using the set command.
The place where you will most likely use the capability of invoking another shell
using the sh command is in debugging shell scripts. This is covered at the end of
this section.

Invoking
A shell is created by entering sh at the command prompt. If it is started with no
arguments, it becomes your current shell, issues a command prompt and
ails for
user input. It is terminated by a control-D or the exit command. You can determine
if you are running multiple shells by using the PS command. For example, if you
typed in the following:
PS
sh
PS
exit
PS
In this sequence. the second ps command shows that you have one more shell than
the other ps commands. Assuming you are in your login shell when you typed this
sequence, the first and third ps commands show one shell, the second ps shows two.
If you invoked sh with an argument. that argument is
taken to be a file name that contains shell commands and the contents of that file
are interpreted. In the section on simple scripts, the first example was a script
called ww. It contained:
date
who
We invoked it by adding execute permission to it with the command
chmod +x ww
and then executing it by entering its name.
An alternative is to enter the command:
sh ww

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 40 of 45

May 15, 2012


Copyright 2002-2012

The result is the same and we dont need to set execute permission. For a
production script, entering sh every time is cumbersome but this capability is
very useful for testing new scripts. This brings us to the subject of debugging.

Debugging
When an error occurs in a script the error message is displayed but you may have
no idea which line in the script produced the error. For example. if a script
contained multiple occurrences of the cat command and you received an error from
cat y ou might not be able to tell which occurrence caused the error.
The -v option can be used to print each script line as it is being read by the
shell. As an example. lets say you create a script called earg that consists of:
# earg
echo $1
echo $2
If you enter...
sh earg first second
the following will be displayed:
first
second
If you enter...
sh -v earg first second
the following will be displayed:
echo $1
first
echo $2
second
Another useful debugging option is -x. This option displays commands as they are
to be executed, after substitution has occurred.
Using the same script as above, if you entered the command
sh -x earg first second

your output would be:


+echo first
first
+echo second second
The + is prepended to the command lines by the shell to show you that these are
the lines after substitution. Or, if you enter the command
sh -xv earg first second
you get:
echo $1
+echo first
first
echo $2
+echo second second
A third option useful for debugging is -u. This causes a reference to an unset
variable to be treated as an error. This option is handy for finding spelling
errors in variable names within a script. Again, Using earg. if you enter the
command sh -u earg first second, the script executes as usual, but if you left off
the second argument to earg and enter
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 41 of 45

May 15, 2012


Copyright 2002-2012

Linux Bash Command Reference Sheet


sh u earg first
your output is:
first
earg: 2: parameter not set
This message tells you that there was a reference to shell variable 2. which was
not set. If you use both the -u and the -v options, you would see the line w here
the error occurred.

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Pg 42 of 45

May 15, 2012


Copyright 2002-2012

Filesystem
cd [-] [directory] Change directory
-

:Change to the previous directory you were in


chmod [opts] <mode> <filepattern> Change permissions
-R :Change permissions recursively
chown [-R] <user>[.group] <file> Change ownership
-R :Change ownership recursively
cp [opts] <from> <to> Copy files and directories
-i :Interactive mode. Prompt before overwriting
-p :Preserve file permissions and ownership
-R :Copy directories recursively
df [opts] [device name] Print filesystem usage info
-a :Show all filesystems.
-h :Human readable format. Quantify byte information.
-i :Show inode usage info.
du [opts] [pattern] Show space usage on files and dirs
-c :Produce a grand total for all arguments.
-h :Human readable format. Quantify byte information.
-s :Summarize. Only show a total for each argument.
-S :Do not include size of subdirectories.
find <path> [-name pattern'] Search for a file.
Ex: find /home name index.html perms 644 -ls
gzip [opts] <filepattern> Compress a file or files
-1..9 : Set compression level. 9=highest, 1=lowest.
-d :Decompress file. Same as the gunzip command.
-l :List the statistics for a compressed file.
ln [opts] <tofile> <linkfile> Create a sym/hard link
-s :Create a symbolic link between files. (alias name)
-f :Force creation, even if the link file exists.
ls [opts] [pattern] List file and directory entries
-a :List all files including . and ..
-d :List directories themselves, not their contents.
-l :Long list. Shows permissions and modified time.
-r :Recusively list files in directories.
-S :Sort output by file size.
-u :Sort by the last access time.
-X :Sort by filename extension.
-1 :Print output files one per line.
--time=atime :Show last access timestamp for file.
mkdir [opts] <dirname> Make a new directory
-m mode: Set the initial permissions to mode.
-p :Create parent directories if they dont exist.
mv [-i] <frompattern> <tofile> Move/Rename a file
-i :Interactive move. Prompt before moving files.
rm [opts] <filepattern> Remove a file
-f :Force removal. Dont ask if its ok to remove.
-i :Interactive remove. Prompt before each file.
-r :Recusively delete directories an their contents.
tar [opts] [tarfile] [pattern] Create an archive
c :Create mode. Create a tar archive.
x :Extract mode. Untar archive contents.
t :List mode. List the contents of the archive.
f :Specify a tarfile to use.
v :Verbose mode. Show files being added or untared.
z :Compress. Filter input/output through gzip.
touch [opts] <pattern> Update the timestamp on a file
-a :Only change the access time on the file.
-t :Specify a timestamp to use instead of current time

Information & Print

awk [opts] [exp] pattern scanning and processing language

--date=STRING :display time described by STRING.


--set=STRING :set time described by STRING.
dmesg [opts] Print or control the kernel ring buffer
-c :Clear the contents of the ring buffer.
file [opts] [filepattern] Determine the file type
-z :Try to look inside compressed files.
finger [opts] [userpattern] Show info about system users
-m :Match the exact username specified.
free [opts] Display free and used memory in the system
-b :Display the information in bytes.
hexdump [opts] Show all the characters of a file
-c :Display the input offset in hexidecimal
last [opts] [username] Show last system logins for users
-num :Show last num of sessions.
-a :Display the hostname in the last column.
-d :Translates IP numbers to their hostname.
-f <file> :Use file as last log.
less [opts] [filepattern] View a file a page at a time
-i :Do case insensitive searching.
-S :Dont wrap long lines.
man [opts] [section] <manpage> View online manual pages.
-a :View all available manual pages for name.
-k string :Search for the specified string.
md5sum [opts] [filepattern] Show the uniqueness of files
ps [opts] Show what processes are running on the system
a :Select all processes on a terminal.
u :Display user oriented format. More columns.
x :Select processes without a controlling TTY.
w :Show an extra line of process entry per w.
Ex: ps auxwww =Displays all process information on system.
quota [opts] [user] Display disk usage and limits
-v :Display filesystems where no quota is set.
time [opts] [command] Show resource usage for a command
top [opts] Display top CPU processes every X seconds
-d sec :Set the delay to sec seconds before refreshing.
uname [opts] Show OS and system information
-a :Show everything
uptime Show system uptime and load
w [opts] [user] Show who is logged in/what they are doing
whereis [command] Locate the related files for a command
which [command] Show full path to the specified command
who [opts] [args] Show who is logged in

-Ffs :Set the field separator for commands.


Ex: cat access_log | awk {print $1} (prints hostnames)
Do a man awk for more information and examples.
cat [opts] [filepattern] Print file contents on STOUT
-E :Display a $ at the end of each line.
-T :Show tabs as ^I.
-v :Show non-printing characters.
comm [opts] [file1] [file2] Compare two sorted files
-1 :Suppress lines unique to left file.
-2 :Suppress lines unique to right file.
-3 :Supress lines unique to both files.
csplit [opts] [file] [pattern] Split a file on context
-f prefix :Use prefix instead of xx in output filenames.
-n digits :Use digits number of digits instead of 2.
-z :Remove empty output files.
Ex: csplit mailspoolfile "/^From /" {*}
cut [opts] [filepattern] Remove sections from each line
-c range :Output only the characters in range
Ex: cut c 1-80 file
(truncate lines at 80 characters)
diff [opts] [file1] [file2] Differentiate two files
Ex: diff program-old.c program.c > program.patch
echo [opts] [string] Print a line of text
-e :Enable interpretation of backslashed sequences.
-n :Dont automatically insert a newline character.
grep [opts] [pattern] [file] Print lines matching pattern
-B num :Print num lines of leading context on matches.
-C num :Print num lines of trailing context on matches.
-E :Interpret pattern as an extended regular expression
-i :Do case insensitve matching.
-l :Just print the files that match the pattern.
-r :Read all files under each directory recursively.
-v :Print the lines that dont match pattern.
head [opts] [file] Print the first part of a file
-n num :Print the first num lines instead of the first 10.
nl [opts] [file] Number the lines of a file
patch [opts] <[patchfile] Patch a file using a diff file
sed [expression] [file] Stream editor
Ex: cat file | sed s/frompattern/topattern/ > output
sort [opts] [file] Sort lines of text files
-n :Compare according to string numerical value.
-r :Reverse the result of comparisons.
split [opts] [file] Split a file into pieces
-l num :Put num lines per output file.
tail [opts] [file] Print the last lines of a file
-f :Output appended data as the file grows.
-n num :Print last num lines of a file instead of last 10
tr [opts] <set1> [set2] Translate char. from set1 to set2
Ex: cat index.html | tr A-Z a-z > index-new.html
uniq [opts] [input] [output] Remove duplicate lines
-c :Prefix lines with number of occurances.
-d :Only print duplicated lines.
-u :Only print unique lines.
wc [opts] [file] Print the number of lines in files, etc.
-m :Print the character count.
-l :Print the line count.
-w :Print the word count.
-L :Print the length of the longest line.

Communication & Network


ifconfig [devicename] [action] [options]
ipchains [opts] Manip. ipchains firewall(kernel 2.2+)
iptables [opts] Manip. iptables firewall(kernel 2.4+)
mail [opts] [address] Send mail from the command line
-s subject :Specify the subject as subject.
-c list
:Send carbon copy to list of users.
-b list
:Send blind carbon copy to list of users.
Ex: echo Meet me at noon. | mail s Reminder c \
bob@company.com,suzy@company.com jack@company.com
netstat [opts] Print network connections and info
-a :Show both listening and non-listening sockets.
-n :Do not attempt to resolve IP addresses.
-t :Only show tcp socket connection table.
ping [opts] [host] Send ICMP packets to network hosts
-c count :Send count number of packets and then quit.
-i sec
:Wait sec seconds between sending packets.
route [opts] [target] Show/Manipulate IP routing table
-n :Show numerical addresses instead of hostnames.
scp [opts] [[host:]fromfile] [[host:]to] Secure copy
-C :Compresses the data that is sent over the session.
-r :Recursively copy directories.
ssh [opts] [[user@]host] [command] Secure shell/login
-C :Compresses the data that is sent over the session.
sniffit [opts] Record TCP network traffic
-i :Interactive mode. Shows all traffic.
-l <length> :Set the maximum sniff length to length.
-p <port> :Specify the port number on the target host.
-s <source> :Set the source IP from which packets come.
-t <target> :Set the IP to which packets are going.
tcpdump [opts] [expression] Dump traffic on a network
telnet [opts] [host] [port] Open TCP socket to a host
-n <file> : Opens file for recording trace information.
-x :Turns on encryption of the data stream if possible.
traceroute [opts] [host] Show the route packets take
-n :Dont do DNS lookups of the IP addresses.
wget [opts] [URL] Make a HTTP request from the shell
-r :Recursive get the URL and all its links.
-k :Convert the non-relative links to relative ones.
whois [opts] <arg[@server]> Query a whois database
Ex: whois domain.com
whois domain.com@whois.networksolutions.com
whois 127.0.0.1@arin.net

NeatInfo.com - by: Jan Zumwalt


BASH SHELL Reference

Text - Edit / Search / Filter

date [opts] Print or set the system date and time

Bash Shell

Admin

Send STDOUT to a file. overwrite/create a file


Ex: ls l > list-of-files.txt
>newfile
>> Send STDOUT to a file, appending to te end of the file
Ex: ps aux > pslog.txt
date >> pslog.txt
| Send the STDOUT from a command to the STDIN of another
Ex: cat listofnames | sort
cat access_log | awk {print $1} | sort | uniq
2> Send STDERR to a file, overwriting the filename
Ex: startx 2> .errorlog
alias Create a command alias in the shell
Ex: alias ls=ls la -color=auto
cd [-] [directory] Change the current working directory
- :Change to the previous directory you were in.
clear Clear the terminal display
env [opts] [command] Run command in modified environment
export [opts] [variable] Export an environment variable
Ex: export TERM USERNAME PS1 MAILSPOOL
export TERM=vt100
for Execute sequence of commands for a list of items
Ex: for i in *.mp3 ; do mpg123 $i ; done
for n in 1 2 3 4 ; do mkdir $n ; done
history Show the command history up til now
nice [opts] [command] Set the OS process priority
Ex: nice 19 gzip access_log
(lowest priority on Linux)
Ex: nice 20 kswapd
(real time priority on Linux)
pwd Print out the current working directory
renice [opts] <arg> Change priority of a running process
-p PID: Specify a process id to renice
Ex: $ ps auxw | grep gzip
6319 ?
S 0:20 gzip bigfile.txt
$ renice 19 p 6319
reset Initializes the terminal as if you just logged in
set
Set a shell option or variable (run help set)
sleep Pause for specified period before continuing
Ex: ps aux ; sleep 3600 ; ps aux
umask Set the default file permissions
Ex: umask 022
(files will be created 644 by default.)
while A loop that runs commands while a condition is true
Ex: while (true) ; do ps auxw ; sleep 1m ; done > pslog
xargs [opts] [command] Execute a command for each arg
-n number :How many arguments to give each command run.
-p :Prompt the user before each command is run.

adduser [opts] <username> Add a user to the local system

>

Pg 43 of 45

-d
-g
-G
-s

<dir> : Set the home directory for the user to dir.


<group> : Set the primary group for the user to group.
<group,group,> : Set additional groups for the user.
<shell> : Set the default shell for the user to shell.
chfn Change the finger information for a user
chsh Change the shell used for the user
edquota [opts] <user> Edit a users or groups quota
-g : Edit the group quota instead of user quota.
kill [-signal] <pid> Terminate a process/Send it a signal
-HUP,-1 : Signal usually makes process to reread config.
-9 :Send a SIGKILL, process must die.
-l :Print a list of signal names and numbers.
killall [-signal] [name]Kill processes by name
-e :Require an exact name of a process.
-i :Interactively ask for confirmation before killing.
ldd [opts] [program] Show a programs library dependencies
mount [opts] <device> [mountpoint] Mount a file system
-o <opts> : Specify options for mounting. Listed below.
loop
remount
ro, rw
user

Mount a disk file such as a CD-ROM image or floppy image.


Remount the filesystem with new options
Mount filesystem in readonly or read-write mode.
Allow normal users to mount this filesystem.

-r :Mount the filesystem read-only. Same as o ro


-t <vfstype> :Specify the type of filesystem to mount.
ext2, ext3
vfat
iso9660
nfs

Native Linux partition types.


Windows 9x 32-bit partition type.
CD-ROM filesystem.
Network remote filesystem.

passwd [opts] [username]Change users system password


-l :Lock the password for the account.
-u :Unlock the password for the account.
-S :Show the status of the password for the account.
su [-] [username] Switch users or login as the superuser
- :Make shell a login shell
-c <command> : Run command as username.
umask [opts] [mode] Set the default file permissions
-S :Show current symbolic umask.
umount [opts] [path/device]Unmount a mounted filesystem
-f :Force unmounting (in case of unreachable NFS system).

May 15, 2012


Copyright 2002-2012

Notes
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 44 of 45

May 15, 2012


Copyright 2002-2012

Notes
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference

Pg 45 of 45

May 15, 2012


Copyright 2002-2012

Anda mungkin juga menyukai