You are on page 1of 12

Bash Quick References

B ash (B our ne A gain Shell) is a shell lang uage build on - top of t he or ig nal B our ne Shell
w hich was dis t r ibu te d w i t h V 7 Uni x in 1979 and be c ame t he s t andard for w r i t ing
shell s cr ipt s . Today i t is pr imar y to mos t L inu x dis t r ibu t ions , Mac OS and i t has e ven
re cent l y be en enable d to r un on W indow s t hroug h s ome t hing c alle d W SL ( W indow s
Subs y s tem for L inu x).

File Te s t Op erator s
Te s t ing file s in s cr ipt s is eas y and s t raig ht for ward. T his is w here shell s cr ipt ing
s t ar t s to show i t s g lor y ! In B ash you c an do file te s t ing for per mis sions , size, date,
file t y pe or e x is tence.
Fl ag D esc r i p t io n
-e F ile e x is t s
-a F ile e x is t s (ident ic al to - e bu t is depre c a te d and ou tda te d)
-f F ile is a reg ular file (not a dire c tor y or de v ice file)
-s file is not zero size
-d file is a dire c tor y
-b file is a blo ck de v ice
-c file is a charac ter de v ice
-p file is a pipe
-h file is a s y mbolic link
-L file is a s y mbolic link
-S file is a s o cke t
file (de s cr iptor) is as s o ciate d w i t h a ter minal de v ice; t his
-t te s t opt ion may be us e d to che ck w he t her t he s tdin [ - t 0 ]
or s tdou t [ - t 1 ] in a g i ven s cr ipt is a ter minal
-r file has read per mis sion (for t he us er r unning t he te s t)
-w file has w r i te per mis sion (for t he us er r unning t he te s t)
-x file has e xe cu te per mis sion (for t he us er r unning t he te s t)
-g s e t- g roup - id (sg id) flag s e t on file or dire c tor y
-u s e t- us er- id (suid) flag s e t on file.
-k s t ick y bi t s e t .
-O you are ow ner of file
-G g roup - id of file s ame as your s
-N file mo di fie d since i t was las t read
f1 - nt f 2 file f 1 is ne wer t han f 2
f1 - ot f 2 file f 1 is older t han f 2
f1 - e f f 2 file s f 1 and f 2 are hard link s to t he s ame file
! Not – re ver s e s t he s ens e of t he te s t s above (re t ur ns t r ue i f
condi t ion abs ent).
Inte ger Comparison Op erator s
How to comp are integer s or ar i t hme t ic e x pre s sions in shell s cr ipt s .
Fl ag D esc r i p t io n
-eq is e qual to
- ne is not e qual to
-gt is g reater t han
- ge is g reater t han or e qual to
-lt is le s s t han
- le is le s s t han or e qual to
< is le s s t han – place w i t hin double p arent he s e s
<= is le s s t han or e qual to (s ame r ule as pre v ious row)
> is g reater t han (s ame r ule as pre v ious row)
>= is g reater t han or e qual to (s ame r ule as pre v ious row)

String Compari son Op erator s


St r ing comp ar is on in B ash.
Fl ag D esc r i p t io n
= is e qual to
== s ame as ab ove
!= is not e qual to
< is le s s t han A S CII alphab e t ic al order
> is g rea ter t han A S CII alphab e t ic al order
-z s t r ing is null (i.e. zero leng t h)
-n s t r ing is not null (i.e. ! zero leng t h)

Comp ound Op erator s


Us e f ul for boolean e x pre s sions and is similar to && and ||. T he compound operator s
wor k w i t h t he te s t command or may occur w i t hin sing le bracke t s [ < e x pr > ].
Fl ag D esc r i p t io n
-a log ic al and
-o log ic al or

Job I denti fier s


Job cont rol allow s you to s ele c t i vel y s top (suspend) t he e xe cu t ion of proce s s e s and
cont inue t heir e xe cu t ion at a la ter point in t ime.
N ot at io n D esc r i p t io n
%N Job number [N]
%S Invoc a t ion (command - line) of job b eg ins w i t h s t r ing S
%? S Invo c a t ion (command - line) of job cont ains w i t hin i t s t r ing S
%% "cur rent " job (las t job s toppe d in foreg round or s t ar te d in b ack g round)
%+ "cur rent " job (las t job s toppe d in foreg round or s t ar te d in b ack g round)
%- L as t job
%! L as t b ack g round pro ce s s
Lis t Cons truc t s
Prov ide s a means of pro ce s sing commands cons e cu t i vel y and in e ffe c t is able to
replace comple x i f/ t hen /c as e s t r uc t ure s .
Co ns t r u c t D esc r i p t io n
&& and cons t r uc t
|| or cons t r uc t

Re ser ve d E x it Code s
Us e f ul for debug g ing a s cr ipt . E x i t t ake s integer ar g s in t he range 0 -2 55.
E x it Co d e N o. D esc r i p t io n
1 C a tchall for general er ror s
2 Misus e of shell buil t ins
126 Command invoke d c annot e xe cu te
127 Command not found
128 Invalid ar g ument to e x i t
128+n Fat al er ror sig nal "n"
13 0 S cr ipt ter mina te d by Cont rol - C

Signal s
UNI X Sy s tem V Sig nals .
Na m e Number Ac t io n D esc r i p t io n
SIG HUP 1 exit Hang s up
SIG IN T 2 exit Inter r upt s .
SIGQUI T 3 core dump Q ui t s .
SIG IL L 4 core dump Illegal ins t r uc t ion.
SIG T R A P 5 core dump Trace t rap.
SIG IOT 6 core dump IOT ins t r uc t ion.
SIG EM T 7 core dump M T ins t r uc t ion.
SIG F PE 8 core dump F lo a t ing point e xcept ion.
SIG K IL L 9 exit K ills (c annot be c aug ht or ig nore d).
SIG BUS 10 core dump B us er ror.
SIGSEG V 11 core dump S eg ment at ion v iolat ion.
SIGS Y S 12 core dump B ad ar g ument to s y s tem c all.
SIG PIPE. 13 exit Wr i te s on a pipe w i t h no one to read i t .
SIG A L R M 14 exit A lar m clo ck .
SIG T ER M 15 exit S of t ware ter mina t ion sig nal.
S ending Control Signal s
You c an us e t he s e key - combina t ions to s end sig nals
Key Co m b o D esc r i p t io n
Ct r l+ C T he inter r upt sig nal , s ends SIG IN T to t he job r unning in t he foreg round.
Ct r l+Y T he delaye d suspend charac ter. C aus e s a r unning proce s s to be s toppe d
w hen i t a t tempt s to read inpu t f rom t he ter minal. Cont rol is re t ur ne d to
t he shell , t he us er c an foreg round , b ack g round or k ill t he proce s s . De -
laye d suspend is onl y available on opera t ing s y s tems suppor t ing t his
fea t ure.
Ct r l+Z T he suspend sig nal , s ends a SIG T S T P to a r unning prog ram , t hus s top -
ping i t and re t ur ning cont rol to t he shell.

C he ck your s t t y s e t t ing s . Suspend and re sume of ou t pu t is usuall y dis able d i f you


are using "moder n" ter minal emula t ions . T he s t andard x ter m suppor t s Ct r l+S and
Ct r l+ Q by de faul t .

File Ty p e s
T his is ver y di fferent f rom W indow s bu t s t raig ht for ward once you ge t i t . I 'll e x p and
t his s e c t ion s oon w i t h more conte x t .
Symbol M e a n i ng
- Reg ular file
d Dire c tor y
l (Sy mb olic) L ink
c C harac ter de v ice
s S o cke t
p Name d pip e
b B lo ck de v ice

Permi s sions
Now you may k now w ha t t ha t arc ane look ing s t r ing r w x r w x r w x is w hen you invoke ls
-l de
Co D esc r i p t io n
s s e t uid w hen in us er column
s s e t g id w hen in g roup column
t s t ick y bi t
0 T he acce s s r ig ht t ha t is suppos e d to be on t his place is not g rante d.
-
4 read acce s s is g rante d to t he us er c ategor y de fine d in t his place
r
2 w r i te per mis sion is g rante d to t he us er c ategor y de fine d in t his place
w
1 e xe cu te per mis sion is g rante d to t he us er c ategor y de fine d in t his place
x
u us er p er mis sions
g g roup p er mis sions
o ot her s p er mis sions
Sp e cial File s
F ile s t hat are read by t he shell. L is te d in order of t heir e xe cu t ion .
Fi l e I nf o
/e tc /profile E xe cu te d au tomat ic all y at log in
~.b ash _ profile
~/.b ash _ log in W hiche ver is found fir s t is e xe cu te d at log in .
~.profile
~/.b ashrc Is read by e ver y nonlog in shell.

String Manipulation
B ash suppor t s a sur pr ising l y big number of s t r ing opera t ions! Unfor t una tel y, t he s e
to ols lack a uni fie d fo cus . S ome are a subs e t of p arame ter subs t i t u t ion , and ot her s
fall under t he f unc t ionali t y of t he UNI X e x pr command. T his re sul t s in inconsis tent
command s y nt a x and over lap of f unc t ionali t y.

Mac OS buil t- in b ash is f rom 20 07 and won' t suppor t many of t he s e.


Pat t e r n Desc r i pt ion
$ {# var} F ind t he leng t h of t he s t r ing
$ {var % p a t ter n} Remove f rom shor tes t rear (end) pat ter n
$ {var %% p a t ter n} Remove f rom longes t rear (end) pat ter n
$ {var :p osi t ion} E x t rac t subs t r ing f rom $var at $posi t ion
$ {var :num1:num2} Subs t r ing
$ {var # p a t ter n} Remove f rom shor tes t f ront pat ter n
$ {var # # p a t ter n} Remove f rom longes t f ront pat ter n
$ {var/p a t ter n /s t r ing } F ind and replace (onl y replace fir s t occur rence)
$ {var//p a t ter n /s t r ing } F ind and replace all occur rences
$ { !pre fi x * } E x pands to t he names of var iables w hose names begin
w i t h pre fi x .
$ {var,} Conver t fir s t charac ter to lowerc ase.
$ {var,p at ter n}
$ {var, ,} Conver t all charac ter s to lowerc ase.
$ {var, , p at ter n}
$ {var^} Conver t fir s t charac ter to upperc ase.
$ {var^ p a t ter n}
$ {var^ ^} Conver t all charac ter to upperc ase.
$ {var^ ^ p a t ter n}
$ {s t r ing /subs t r ing /replacement} Replace fir s t match of $subs t r ing w i t h $replacement
$ {s t r ing //subs t r ing /replacement} Replace all matches of $subs t r ing w i t h $replacement
$ {s t r ing /# subs t r ing /replacement} I f $subs t r ing matches f ront end of $s t r ing , subs t i t u te
$replacement for $subs t r ing
$ {s t r ing / % subs t r ing /replacement} I f $subs t r ing matches back end of $s t r ing , subs t i t u te
$replacement for $subs t r ing
e x pr match " $s t r ing " ' $subs t r ing ' Leng t h of matching $subs t r ing * at beginning of $s t r ing
e x pr " $s t r ing " : ' $subs t r ing ' Leng t h of matching $subs t r ing * at beginning of $s t r ing
Pat t e r n Desc r i pt ion
e x pr inde x " $s t r ing " $subs t r ing Numer ic al posi t ion in $s t r ing of fir s t charac ter in $sub -
s t r ing * t hat matches [0 i f no match , fir s t charac ter count s
as posi t ion 1]
e x pr subs t r $s t r ing $p osi t ion E x t rac t $leng t h charac ter s f rom $s t r ing s t ar t ing at $posi -
$leng t h t ion [0 i f no match , fir s t charac ter count s as posi t ion 1]
e x pr ma tch " $s t r ing " ' \ ($sub - E x t rac t $subs t r ing *, searching f rom beginning of $s t r ing
s t r ing \)'
e x pr " $s t r ing " : ' \ ($subs t r ing \)' E x t rac t $subs t r ing * , searching f rom beginning of $s t r ing
e x pr ma tch " $s t r ing " '.* \ ($sub - E x t rac t $subs t r ing *, searching f rom end of $s t r ing
s t r ing \)'
e x pr " $s t r ing " : '.* \ ($subs t r ing \)' E x t rac t $subs t r ing *, searching f rom end of $s t r ing

Quoting
T he follow ing te x t show s charac ter s or t hat ne e d to be quote d i f you want to us e
t heir li teral s y mbols and not t heir spe cial meaning.
Symbol Lit e ra l M e a n i ng
; Command s ep era tor
& B ack g round e xe cu t ion
() Command g rouping
| Pip e
< > & Re dire c t ion s y mb ols
* ? [ ] ~ + - @ ! F ilename me t acharac ter s
" ' \ Us e d in quot ing charac ter s
$ Var iable, command or ar i t hme t ic subs t i t uion
# St ar t a command t hat ends on a linebreak
sp ace t ab ne w line Word s ep era tor s
Ever y t hing be t we en "..." is t aken li terall y, e xcept $ (dollar) ` (b ack t ick) and " (dou -
ble - quot a t ion).

E ver y t hing b e t we en '...' is t aken li terall y, e xcept ' (sing le - quote).

T he charac ter s follow ing \ is t aken li terall y.


Us e \ to e s c ape any t hing in "..." or '...'

Using $ be fore "..." or '...' c aus e s s ome spe cial behav ior. $"..." is t he s ame as "..."
e xcept t hat loc ale t ransla t ion is done. L ike w is e, $'...' is similar to $'...' e xcept t hat t he
quote d s t r ing is proce s s e d for e s c ape s e quence s .

Command Parameter s
Command p arame ter s , als o k now n as ar g ument s , are us e d w hen invok ing a B ash s cr ipt .
Co m ma n d D esc r i p t io n
$0 Name of t he s cr ipt i t s el f
$1 … $9 Parame ter 1 ... 9
$ {10} Posi t ional p arame ter 10
$* E x p ands to t he posi t ional p arame ter s , s t ar t ing f rom one.
W hen t he e x p ansion occur s w i t hin double quote s , i t
e x p ands to a sing le word w i t h t he value of each p arame ter
s eperate d by t he fir s t of t he IF S env ironment var iable
Co m ma n d D esc r i p t io n
$- Cur rent opt ions
$_ T he under s core var iable is s e t a t shell s t ar t up and cont ains t he abs olu te
file name of t he shell or s cr ipt being e xe cu te d as p as s e d in t he ar g ument
lis t . Subs e quent l y, i t e x p ands to t he las t ar g ument to t he pre v ious com -
mand , af ter e x p ansion . I t is als o s e t to t he f ull p at hname of each command
e xe cu te d and place d in t he env ironment e x por te d to t hat command. W hen
che ck ing mail , t his p arame ter holds t he name of t he mail file.
$$ Pro ce s s id of t he shell
$? E x i t s t at us of t he mos t re cent l y e xe cu te d command
$@ A ll ar g ument s as s ep arate words
$# Number of ar g ument s
$! PID of mos t re cent l y b ack g rounde d pro ce s s

His tor y E x pansion


Enable s us e and manipulat ion of pre v ious commands .
Co m ma n d D esc r i p t io n
! St ar t s a his tor y subs t i t u t ion
!! Re fer s to t he las t command.
!n Re fer s to t he <n> - t h command line.
!-n Re fer s to t he cur rent command line minus <n>.
!s t r ing Re fer s to t he mos t re cent command s t ar t ing w i t h <s t r ing >
! ? s t r ing? Re fer s to t he mos t re cent command cont aining <s t r ing > (t he ending
? is opt ional)
^ s t r ing1^ s t r ing 2^ Q uick subs t i t u t ion . Repeat s t he las t command , replacing <s t r ing1>
w i t h <s t r ing 2>.
!# Re fer s to t he ent ire command line t y pe d s o far.

Variable Op erations
Per for m operat ions on var iable s .
E x p r ess io n
$ {p arame ter :- de faul t Value}
G e t de faul t shell var iable s value
$ {p arame ter := de faul t Value}
S e t de faul t shell var iable s value
$ {p arame ter :? " Er ror Me s s age" }
Display an er ror me s s age i f p arame ter is not s e t

B a sh Globbing
B ash c annot re cog nize RegE x bu t under s t and g lobbing. G lobbing is done to filename s
by t he shell w hile RegE x is us e d for s earching te x t .
Glob D esc r i p t io n
* Matche s zero or more o ccurence s of a g i ven p at ter n
? Matche s zero or one o ccurence s of a g i ven p at ter n
+ Matche s one or more o ccurence s of a g i ven p at ter n
! Negate s any p at ter n matche s — re ver s e s t he p at ter n s o to
sp eak t
Re gular E x pre s sions
A l way s us e quote s in your RegE x to avoid g lobbing
O p e rat o r Effect
. Ma tche s any sing le charac ter.
? T he pre ce ding i tem is opt ional and w ill be matche d , at mos t , once.
* T he pre ce ding i tem w ill be ma tche d zero or more t ime s .
+ T he pre ce ding i tem w ill be ma tche d one or more t ime s .
{N} T he pre ce ding i tem is ma tche d e xac t l y N t ime s .
{N ,} T he pre ce ding i tem is ma tche d N or more t ime s .
{N , M} T he pre ce ding i tem is ma tche d at leas t N t ime s , bu t not more t han
M t ime s .
- Repre s ent s t he range i f i t 's not fir s t or las t in a lis t or t he ending
point of a range in a lis t .
^ Ma tche s t he empt y s t r ing at t he beg inning of a line; als o repre s ent s
t he charac ter s not in t he range of a lis t .
$ Ma tche s t he empt y s t r ing at t he end of a line.
[ao ei AOEI ] Ma tche s any 1 charac ter f rom t he lis t .
[^AOEIao ei ] Ma tche s any 1 charac ter, not in t he lis t !
[a -f ] Ma tche s any 1 charac ter in t he range a -f
In b asic reg ular e x pre s sions t he me t acharac ter s " ? ", "+", "{ ", "|", "( ", and " )" los e t heir
sp e cial meaning ; ins tead us e t he b ack slash ver sions " \? " ... " \)". C he ck in your s y s tem
document at ion w he t her commands using reg ular e x pre s sions suppor t e x tende d
e x pre s sions .

Charac ter Cla s se s In BR E


A charac ter clas s [:C har C las s:] is a s e t of pre de fine d p at ter ns and compr pis e d of t he
follow ing :
Cha rac t e r Class E q u i va l e nt E x p l a nat io n
[:lower :] [a -z] Lowerc as e le t ter s .
[:upp er :] [A -Z ] Upp erc as e le t ter s .
[:alpha:] [A -Z a -z] A lphab e t ic le t ter s , b ot h upp er- and lowerc as e.
[:dig i t :] [0 - 9] Numb er s 0 - 9.
[:alnum:] [a -z A -Z0 - 9] A lphanumer ic : b ot h le t ter s (upp er- + lowerc as e) and dig i t s
[: xdig i t :] [0 - 9A - Fa -f ] He xade cimal dig i t s .
[:sp ace:] [ \t \ n\ r \f \v] W hi te sp ace. Sp ace s , t abs , ne w line and similar.
[:punc t :] Sy mb ols (minus dig i t s and le t ter s).
[:pr int :] [ [:g raph] ] Pr int able charac ter s (sp ace s include d).
[:blank :] [ \t] Sp ace and t ab charac ter s onl y.
[:g raph:] [^ [:cnt r l:] ] G raphic all y pr int able charac ter s e xcluding sp ace.
[:cnt r l:] Cont rol charac ter s . Non - pr int able charac ter s .
Shell Builtins
Shell builins are buil t into B ash are of ten ver y (i f not e x t remel y) fas t comp are d to
e x ter nal prog rams . S ome of t he buil t ins are inher i te d f rom t he B our ne Shell (sh) —
t he s e inher i te d commands w ill als o wor k in t he or ig inal B our ne Shell.

Builtin D esc r i p t io n
: Equi valent to t r ue.
. Reads and e xe cu te s commands f rom a de sig nate d file in t he cur rent shell.
[ Is a s y nony m for te s t bu t re quire s a final ar g ument of ].
alias D e fine s an alias for t he spe ci fie d command.
bg Re sume s a job in b ack g round mode.
bind Binds a key bo ard s e quence to a read line f unc t ion or macro.
break E x i t s f rom a for, w hile, s ele c t , or unt il loop.
buil t in E xe cu te s t he spe ci fie d shell buil t- in command.
c aller Re t ur ns t he conte x t of any ac t i ve subrou t ine c all
c as e
cd C hange s t he cur rent dire c tor y to t he spe ci fie d dire c tor y.
command E xe cu te s t he spe ci fie d command w i t hou t t he nor mal shell lo ok up.
compgen G enerate s pos sible comple t ion matche s for t he spe ci fie d word.
comple te Display s how t he spe ci fie d words would be comple te d.
comopt
cont inue Re sume s t he ne x t i tera t ion of a for, w hile, s ele c t , or unt il loop.
de clare De clare s a var iable or var iable t y pe.
dir s Display s a lis t of cur rent l y rememb ere d dire c tor ie s .
dis ow n Remove s t he spe ci fie d jobs f rom t he jobs t able for t he pro ce s s .
e cho Display s t he spe ci fie d s t r ing to S T D OU T.
enable Enable s or dis able s t he spe ci fie d buil t- in shell command.
e val Conc atenate s t he spe ci fie d ar g ument s into a sing le command , and e xe cu te s
t he command.
e xe c Replace s t he shell pro ce s s w i t h t he spe ci fie d command.
exit F orce s t he shell to e x i t w i t h t he spe ci fie d e x i t s t at us .
e x p or t S e t s t he spe ci fie d var iable s to be available for child shell pro ce s s e s .
fc S ele c t s a lis t of commands f rom t he his tor y lis t .
fg Re sume s a job in foreg round mode.
ge topt s Par s e s t he spe ci fie d posi t ional p arame ter s .
hash F inds and remember s t he f ull p at hname of t he spe ci fie d command.
help Display s a help file.
his tor y Display s t he command his tor y.
if Us e d for branching.
jobs L is t s ac t i ve jobs .
k ill S ends a s y s tem sig nal to t he spe ci fie d pro ce s s ID (PID).
le t Evaluate s each ar g ument in a ma t hemat ic al e x pre s sion.
lo c al Create s a limi te d - s cope var iable in a f unc t ion .
logou t E x i t s a log in shell.
mapfile
p op d Remove s ent r ie s f rom t he dire c tor y s t ack .
Builtin D esc r i p t io n
pr int f Display s te x t using for mat te d s t r ing s .
pushd Adds a dire c tor y to t he dire c tor y s t ack .
pwd Display s t he p a t hname of t he cur rent wor k ing dire c tor y.
read Reads one line of da t a f rom S T DIN , and as sig ns i t to a var iable.
readonl y Reads one line of da t a f rom S T DIN , and as sig ns i t to a var iable t hat c an ’ t
b e change d.
re t ur n F orce s a f unc t ion to e x i t w i t h a value t hat c an be re t r ie ve d by t he c alling
s cr ipt .
set S e t s and display s env ironment var iable value s and shell at t r ibu te s .
shi f t Rot a te s p osi t ional p arame ter s dow n one posi t ion.
shopt Tog g le s t he value s of var iable s cont rolling opt ional shell b ehav ior.
s ource Reads and e xe cu te s commands f rom a de sig nate d file in t he cur rent shell.
susp end Susp ends t he e xe cu t ion of t he shell unt il a SIGCON T sig nal is re cei ve d.
te s t Re t ur ns an e x i t s t a t us of 0 or 1 b as e d on t he spe ci fie d condi t ion.
t ime s Display s t he accumula te d us er and s y s tem shell t ime.
t rap E xe cu te s t he sp e ci fie d command i f t he spe ci fie d s y s tem sig nal is re cei ve d.
t y pe Display s how t he sp e ci fie d words would be inter pre te d i f us e d as a com -
mand.
t ypeset De clare s a var iable or var iable t y pe.
ulimi t S e t s a limi t on t he sp e ci fic re s ource for s y s tem us er s .
umask S e t s de faul t p er mis sions for ne w l y create d file s and dire c tor ie s .
unalias Remove s sp e ci fie d alias .
uns e t Remove s t he sp e ci fie d env ironment var iable or shell at t r ibu te.
unt il Loop t ha t is ver y similar to t he w hile - loop e xcept t hat i t e xe cu te s unt il t he
te s t- command e xe cu te s succe s f ull y. A s long as t he te s t- command fails , t he
unt il - lo op cont inue s .
wai t Make t he shell wai t for a job to finish.
w hile Wai t s for t he sp e ci fie d proce s s to comple te, and re t ur ns t he e x i t s t at us .
O ver v iew O f B a sh Sy mb ol s
Here we have ga t here d a colle c t ion of all arc ane s y nt a x along w i t h a br ie f de s cr ip -
t ion. A bunch of t he s e s y mb ols are repeate d f rom ear lier bu t many are ne w - t his is a
good s t ar t ing p oint i f you are ne w to t he lang uage.

Symbol Q u i c k Re f e r e n ce
# us e d for comment s
$ $ us e d for p arame ter s and var iable s . Has a bunch of e dge c as e s .
( ) is us e d for r unning commands in a subshell.
$( ) is us e d for s av ing ou t pu t of commands t hat are s end to r un in a
subshell.
(( )) is us e d for ar i t hme t ic .
$ (( )) is us e d for re t r ie v ing t he ou t pu t of ar i t hme t ic e x pre s sions , ei t her
for us age w i t h a command or to s ave t he ou t pu t in a var iable.
$[ ] depre c a te d integer e x p ansion cons t r uc t w hich is replace d by $ ((
)). E valua te s integer s b e t we en t he s quare bracke t s
[ ] is us e d for te s t ing and is a buil t- in. Is us e f ul in s ome c as e s for
filename e x p ansion and s t r ing manipulat ion.
[[ ]] is us e d for te s t ing. T his is t he one you should us e unle s s you c an
t hink of a reas on not to.
<( ) Us e d for pro ce s s subs t i t u t ion and is similar to a pipe. C an b e
us e d w hene ver a command e x pe c t s a file and you c an us e mul t i -
ple a t once.
{ } is us e d for e x p ansion of s e quence s
${ } is us e d for var iable inter polat ion and s t r ing manipulat ion.
| is a pip e w hich is us e d for chaining commands toge t her.
< us e d for fe e ding inpu t to commands f rom a file
> us e d for s ending ou t pu t to a file and erasing any pre v ious con -
tent in t ha t file.
|| log ic al or
&& log ic al and
- us e d for opt ion pre fi xe s
-- us e d for t he long- opt ion pre fi xe s
& us e d to s end a job to t he b ack g round
<< WOR D
<< -WOR D is us e d for here doc s
<<' WOR D'
<< -' WOR D'
<<< is us e d for here s t r ing s
>> is us e d to app end ou t pu t to a file.
' ' sing le quote s are us e d to pre s er ve t he li teral value
" " double quote s are us e d to pre s er ve t he li teral value of all charac-
ter s e xcept $, ` ` and \
\ b ack slash is us e d to e s c ap e ot her w is e inter pre te d s y mb ols /char-
ac ter s w hich has a sp e cial meaning
/ us e d for s ep era t ing t he component s of a filename
Symbol Q u i c k Re f e r e n ce
: similar to a NOP – a do not hing operat ion. I t is a shell buil t in w i t h
an e x i t s t a t us of t r ue
; us e d to s ep era te commands intende d to r un s e quent all y.
, us e d for link ing toge t her ar i t hme t ic op era t ions . A ll are e valu t a t-
e d bu t onl y t he las t is re t ur ne d
. repre s ent s t he cur rent dire c tor y.
.. repre s ent s p arent dire c tor y.
~ e x p ands to home dire c tor y.
` ` is depre c a te d and should not be us e d. Read f ur t her in i t s re sp e c-
t i ve s e c t ion .

Flow Control
F low cont rol s t r uc t ure s in B ash are s t raig ht for ward , albei t B ash is unfor g i v ing i f you
ge t t he s y nt a x w rong.

V ie w e xample s on how to us e cont rol flow in b ash.

S y nt a x A s soc iat e d Key wo r d s D esc r i p t io n


St r u c t u r e o r Key S y m b o l s
If i f t hen fi Te s t a condi t ion.
I f- els e i f t hen els e fi Te s t a condi t ion and us e a fallb ack i f t he te s t
fails .
I f- eli f- els e i f t hen eli f els e fi Prov ide s addi t ional te s t ing plus a fallb ack i f
all te s t s fail. You may sk ip t he eli f condi t ions
or add as many in - be t we en as you like. Simi -
lar l y, you may sk ip t he els e fallb ack .
F or or do done I terate over a s e quence, a lis t or any t hing as
far as t he imag inat ion goe s .
W hile w hile do done W hile a condi t ion is t r ue - repeat unt il t hat
condi t ion is no longer t r ue
Unt il unt il do done T he inver s e of t he w hile loop - as long as t he
te s t- command fails , t he unt il - lo op cont inue s .
S ele c t s ele c t in do done S ele c t s ele c t in do done Us e d for eas y menu
generat ion. A ny s t atement w i t hin c an b e
anot her s ele c t cons t r uc t , t hus enabling
sub - menu crea t ion.
C as e c as e ) ;; e s ac A l ter nat i ve i f- branching. E ach c as e is an
e x pre s sion w hich matche s a g i ven p at ter n
(i.e., a c as e).

FindmoreStationXCheatSheetshere-
h t t p s : / / w w w . s t a t i o n x . n e t / c a t e g o r y / c h e a t - s h e e ts /

You might also like