com/dev_notes/_cheat
http:/neatinfo.com/dev_notes/_cheat-sheets
Notes:
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference
Pg 2 of 45
Table of Contents
Table of Contents ........................................... 3
Commands & Programs - Alphabetical ......... 4
Langauages ..................................................... 4
Quoting ........................................................ 23
Single Quotes ................................................. 23
Double Quotes ................................................ 23
Single Character Escape ................................ 23
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
Shell Functions............................................. 38
Functions vs. Scripts ....................................... 38
Defining a Function ......................................... 38
Where to Define Functions .............................. 39
Status of Definitions ........................................ 39
Search Order................................................ 39
Command Line Shell Invoking ...................... 40
Invoking .......................................................... 40
Debugging.................................................... 41
Linux Bash Command Reference Sheet....... 42
Pg 3 of 45
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
Langauages
cc
f77
make
nasm
pc
perl
Pg 4 of 45
c compiler
fortran compiler
source code manager
assembler
pascal compiler
perl compiler
compress file
file archive program
compress files
manage packages
combine files
expand compressed file
display compressed file
change
change
change
change
directory
group permissions
file/dir permissions
owner permissions
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
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
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
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
;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
Log files
/var/log
;log files
Spool files
/usr/spool
/usr/spool/lp
Pg 7 of 45
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 .......................
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 ..........................
;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
;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
Pg 9 of 45
Special Characters
|
;
&
&&
||
\
?
*
[chars]
[!chars]
<upside down
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
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
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
${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}
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
Pg 11 of 45
$$IFS
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.
$$
$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.
Pg 12 of 45
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
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.
Pg 14 of 45
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
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
Pg 16 of 45
Pg 17 of 45
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
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
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
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.
${var:?word}
${var:+word}
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
Pg 20 of 45
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.
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
[^str]
re*
re\{m,n\}
\(re\)
\n
^
$
<null>
&
%
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.
Pg 22 of 45
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.
Pg 23 of 45
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
Pg 24 of 45
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!
Pg 25 of 45
Pg 26 of 45
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
\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
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.
Pg 29 of 45
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
True
true
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>.
Pg 30 of 45
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:
Numb
er
1
2
3
4
5
6
Symbolic
Name
SIGHUP
SIGINT
SIGQUIT
SIGILL
SIGTRAP
SIGIOT
Hangup
Interrupt
Quit
Illegal instruction
Trace trap
lOT trap
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
Terminate
Ignore
Terminate
Terminate
Terminate
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
Yes
No
No
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
Pg 32 of 45
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
file
file
file
file
exists
exists
exists
exists
and
and
and
and
is
is
is
is
a
a
a
a
Pg 33 of 45
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
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
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
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
Pg 36 of 45
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
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.
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
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 }.
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
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.
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
Pg 40 of 45
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
Pg 41 of 45
Pg 42 of 45
Filesystem
cd [-] [directory] Change directory
-
Bash Shell
Admin
>
Pg 43 of 45
-d
-g
-G
-s
Notes
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference
Pg 44 of 45
Notes
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
__________________________________________________________
NeatInfo.com - by: Jan Zumwalt
BASH SHELL Reference
Pg 45 of 45