Uploaded by corwin0kristine

Bash-Cheat-Sheet

advertisement
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
-e
-a
-f
-s
-d
-b
-c
-p
-h
-L
-S
D esc r i p t io n
F ile e x is t s
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 ile is a reg ular file (not a dire c tor y or de v ice file)
file is not zero size
file is a dire c tor y
file is a blo ck de v ice
file is a charac ter de v ice
file is a pipe
file is a s y mbolic link
file is a s y mbolic link
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
-w
-x
-g
-u
-k
-O
-G
-N
f1 - nt f 2
f1 - ot f 2
f1 - e f f 2
!
file has read per mis sion (for t he us er r unning t he te s t)
file has w r i te per mis sion (for t he us er r unning t he te s t)
file has e xe cu te per mis sion (for t he us er r unning t he te s t)
s e t- g roup - id (sg id) flag s e t on file or dire c tor y
s e t- us er- id (suid) flag s e t on file.
s t ick y bi t s e t .
you are ow ner of file
g roup - id of file s ame as your s
file mo di fie d since i t was las t read
file f 1 is ne wer t han f 2
file f 1 is older t han 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
=
==
!=
<
>
-z
-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
s t r ing is null (i.e. zero leng t h)
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
-o
log ic al and
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
%N
%S
%? S
%%
%+
%%!
D esc r i p t io n
Job number [N]
Invoc a t ion (command - line) of job
Invo c a t ion (command - line) of job
"cur rent " job (las t job s toppe d in
"cur rent " job (las t job s toppe d in
L as t job
L as t b ack g round pro ce s s
b eg ins w i t h s t r ing S
cont ains w i t hin i t s t r ing S
foreg round or s t ar te d in b ack g round)
foreg round or s t ar te d in b ack g round)
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.
1
2
126
127
128
128+n
13 0
D esc r i p t io n
C a tchall for general er ror s
Misus e of shell buil t ins
Command invoke d c annot e xe cu te
Command not found
Invalid ar g ument to e x i t
Fat al er ror sig nal "n"
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
SIG HUP
1
SIG IN T
2
SIGQUI T
3
SIG IL L
4
SIG T R A P
5
SIG IOT
6
SIG EM T
7
Ac t io n
exit
exit
core dump
core dump
core dump
core dump
core dump
D esc r i p t io n
Hang s up
Inter r upt s .
Q ui t s .
Illegal ins t r uc t ion.
Trace t rap.
IOT ins t r uc t ion.
M T ins t r uc t ion.
SIG F PE
SIG K IL L
8
9
core dump
exit
F lo a t ing point e xcept ion.
K ills (c annot be c aug ht or ig nore d).
SIG BUS
SIGSEG V
10
11
core dump
core dump
B us er ror.
S eg ment at ion v iolat ion.
SIGS Y S
SIG PIPE.
12
13
core dump
exit
B ad ar g ument to s y s tem c all.
Wr i te s on a pipe w i t h no one to read i t .
SIG A L R M
SIG T ER M
14
15
exit
exit
A lar m clo ck .
S of t ware ter mina t ion sig nal.
S ending Control Signal s
You c an us e
Key Co m b o
Ct r l+ C
Ct r l+Y
Ct r l+Z
t he s e key - combina t ions to s end sig nals
D esc r i p t io n
T he inter r upt sig nal , s ends SIG IN T to t he job r unning in t he foreg round.
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.
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
d
l
c
s
p
b
M e a n i ng
Reg ular file
Dire c tor y
(Sy mb olic) L ink
C harac ter de v ice
S o cke t
Name d pip e
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
t
s e t g id w hen in g roup column
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
r
read acce s s is g rante d to t he us er c ategor y de fine d in t his place
2
w
1
x
u
g
o
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
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
us er p er mis sions
g roup p er mis sions
ot her s p er mis sions
Sp e cial File s
F ile s t hat are read
Fi l e
/e tc /profile
~.b ash _ profile
~/.b ash _ log in
~.profile
~/.b ashrc
by t he shell. L is te d in order of t heir e xe cu t ion .
I nf o
E xe cu te d au tomat ic all y at log in
W hiche ver is found fir s t is e xe cu te d at log in .
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
$ {# var}
$ {var % p a t ter n}
$ {var %% p a t ter n}
$ {var :p osi t ion}
$ {var :num1:num2}
$ {var # p a t ter n}
$ {var # # p a t ter n}
$ {var/p a t ter n /s t r ing }
$ {var//p a t ter n /s t r ing }
$ { !pre fi x * }
$ {var,}
$ {var,p at ter n}
$ {var, ,}
$ {var, , p at ter n}
$ {var^}
$ {var^ p a t ter n}
$ {var^ ^}
$ {var^ ^ p a t ter n}
$ {s t r ing /subs t r ing /replacement}
$ {s t r ing //subs t r ing /replacement}
$ {s t r ing /# subs t r ing /replacement}
Desc r i pt ion
F ind t he leng t h of t he s t r ing
Remove f rom shor tes t rear (end) pat ter n
Remove f rom longes t rear (end) pat ter n
E x t rac t subs t r ing f rom $var at $posi t ion
Subs t r ing
Remove f rom shor tes t f ront pat ter n
Remove f rom longes t f ront pat ter n
F ind and replace (onl y replace fir s t occur rence)
F ind and replace all occur rences
E x pands to t he names of var iables w hose names begin
w i t h pre fi x .
Conver t fir s t charac ter to lowerc ase.
Conver t all charac ter s to lowerc ase.
Conver t fir s t charac ter to upperc ase.
Conver t all charac ter to upperc ase.
Replace fir s t match of $subs t r ing w i t h $replacement
Replace all matches of $subs t r ing w i t h $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
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 '
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
* ? [ ] ~ + - @ !
" ' \
$
#
sp ace t ab ne w line
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
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
Name of t he s cr ipt i t s el f
$0
Parame ter 1 ... 9
$1 … $9
Posi t ional p arame ter 10
$ {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
!!
!n
!-n
!s t r ing
! ? s t r ing?
^ s t r ing1^ s t r ing 2^
!#
Re fer s to t he las t command.
Re fer s to t he <n> - t h command line.
Re fer s to t he cur rent command line minus <n>.
Re fer s to t he mos t re cent command s t ar t ing w i t h <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)
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
O p e rat o r
.
?
*
+
{N}
{N ,}
{N , M}
^
$
[ao ei AOEI ]
[^AOEIao ei ]
[a -f ]
in your RegE x to avoid g lobbing
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 .
T he pre ce ding i tem is ma tche d e xac t l y N t ime s .
T he pre ce ding i tem is ma tche d N or more t ime s .
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.
Ma tche s any 1 charac ter f rom t he lis t .
Ma tche s any 1 charac ter, not in t he lis t !
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 :]
[:upp er :]
[:alpha:]
[:dig i t :]
[:alnum:]
[: xdig i t :]
[:sp ace:]
[:punc t :]
[:pr int :]
[:blank :]
[:g raph:]
[:cnt r l:]
[a -z]
[A -Z ]
[A -Z a -z]
[0 - 9]
[a -z A -Z0 - 9]
[0 - 9A - Fa -f ]
[ \t \ n\ r \f \v]
[ [:g raph] ]
[ \t]
[^ [:cnt r l:] ]
Lowerc as e le t ter s .
Upp erc as e le t ter s .
A lphab e t ic le t ter s , b ot h upp er- and lowerc as e.
Numb er s 0 - 9.
A lphanumer ic : b ot h le t ter s (upp er- + lowerc as e) and dig i t s
He xade cimal dig i t s .
W hi te sp ace. Sp ace s , t abs , ne w line and similar.
Sy mb ols (minus dig i t s and le t ter s).
Pr int able charac ter s (sp ace s include d).
Sp ace and t ab charac ter s onl y.
G raphic all y pr int able charac ter s e xcluding sp ace.
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
:
.
[
alias
bg
bind
break
buil t in
c aller
c as e
cd
command
compgen
comple te
comopt
cont inue
de clare
dir s
dis ow n
e cho
enable
e val
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 ].
D e fine s an alias for t he spe ci fie d command.
Re sume s a job in b ack g round mode.
Binds a key bo ard s e quence to a read line f unc t ion or macro.
E x i t s f rom a for, w hile, s ele c t , or unt il loop.
E xe cu te s t he spe ci fie d shell buil t- in command.
Re t ur ns t he conte x t of any ac t i ve subrou t ine c all
C hange s t he cur rent dire c tor y to t he spe ci fie d dire c tor y.
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.
G enerate s pos sible comple t ion matche s for t he spe ci fie d word.
Display s how t he spe ci fie d words would be comple te d.
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 s a var iable or var iable t y pe.
Display s a lis t of cur rent l y rememb ere d dire c tor ie s .
Remove s t he spe ci fie d jobs f rom t he jobs t able for t he pro ce s s .
Display s t he spe ci fie d s t r ing to S T D OU T.
Enable s or dis able s t he spe ci fie d buil t- in shell command.
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
exit
Replace s t he shell pro ce s s w i t h t he spe ci fie d command.
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
fc
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 .
S ele c t s a lis t of commands f rom t he his tor y lis t .
fg
ge topt s
Re sume s a job in foreg round mode.
Par s e s t he spe ci fie d posi t ional p arame ter s .
hash
help
his tor y
if
jobs
k ill
le t
lo c al
logou t
mapfile
p op d
F inds and remember s t he f ull p at hname of t he spe ci fie d command.
Display s a help file.
Display s t he command his tor y.
Us e d for branching.
L is t s ac t i ve jobs .
S ends a s y s tem sig nal to t he spe ci fie d pro ce s s ID (PID).
Evaluate s each ar g ument in a ma t hemat ic al e x pre s sion.
Create s a limi te d - s cope var iable in a f unc t ion .
E x i t s a log in shell.
Remove s ent r ie s f rom t he dire c tor y s t ack .
Builtin
pr int f
pushd
pwd
read
readonl y
re t ur n
set
shi f t
shopt
s ource
susp end
te s t
t ime s
t rap
t y pe
t ypeset
ulimi t
umask
unalias
uns e t
unt il
wai t
w hile
D esc r i p t io n
Display s te x t using for mat te d s t r ing s .
Adds a dire c tor y to t he dire c tor y s t ack .
Display s t he p a t hname of t he cur rent wor k ing dire c tor y.
Reads one line of da t a f rom S T DIN , and as sig ns i t to a var iable.
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.
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 .
S e t s and display s env ironment var iable value s and shell at t r ibu te s .
Rot a te s p osi t ional p arame ter s dow n one posi t ion.
Tog g le s t he value s of var iable s cont rolling opt ional shell b ehav ior.
Reads and e xe cu te s commands f rom a de sig nate d file in t he cur rent shell.
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.
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.
Display s t he accumula te d us er and s y s tem shell t ime.
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.
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.
De clare s a var iable or var iable t y pe.
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 .
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 .
Remove s sp e ci fie d alias .
Remove s t he sp e ci fie d env ironment var iable or shell at t r ibu te.
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 .
Make t he shell wai t for a job to finish.
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
#
$
( )
$( )
(( ))
$ (( ))
$[ ]
[ ]
[[ ]]
<( )
{ }
${ }
|
<
>
||
&&
-&
<< WOR D
<< -WOR D
<<' WOR D'
<< -' WOR D'
<<<
>>
' '
" "
\
/
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
is us e d for here doc s
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 character 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 /charac 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 te 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 ct 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
St r u c t u r e
If
I f- els e
A s soc iat e d Key wo r d s
o r Key S y m b o l s
D esc r i p t io n
i f t hen fi
i f t hen els e fi
I f- eli f- els e
i f t hen eli f els e fi
F or
or do done
W hile
w hile do done
Unt il
unt il do done
S ele c t
s ele c t in do done
C as e
c as e ) ;; e s ac
Te s t a condi t ion.
Te s t a condi t ion and us e a fallb ack i f t he te s t
fails .
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 .
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 a condi t ion is t r ue - repeat unt il t hat
condi t ion is no longer t r ue
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 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.
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).
FindmoreStationXCheatSheetshereh 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 /
Download