!"#$%&'()"*+",(-(./01(
BASH cheat sheet - Level 3
Control Structure
Conditional statements
!"#$%&#if#'()(&*&+(,#
#
-.#//#condition1#001(%&+# # 2#34 #23&('5)6&'("4&#
# command1# # #############*5674," + ,(*+ (, 3" , ('8+ ,"9#
&7-.#//#condition2#001(%&+# # ##########
# command2#
&7'&# # # #
# command3#
.-#
( :&4;745(command1(*;(condition1(*'(,4%& <(&=' &*;(, 3& (
condition2(*'(,4%& (,3 &(co mm and2(*'(6&4;74 5 & > <(&='& (*,?'(,3 & (
command3(,3 ",(@ *==(A&(6&4;745 &> B(
#
8-7&#(&'(',#
# 9.#file# 24%&(*;(file(;*=&(&9*','(
# 9:#dir(( 24%&(*;(dir(>*4(&9*','(
( 9;#'(<-+=# 24%&(*;(',4*+C(*'(&56,8B#
# 9+#'(<-+=# 24%&(*;(',4*+C(*'(+7+ -&56,8B(
# #
# file1#>+(#file2##
24%&(*;(file1(3"'(A&&+(#3"+C&>(574&(4&#&+,=8(
,3"+(file2<(74(*;(file1 (&9*','("+>(file2(>7&'(+7,B(
# file1#>?(#file2#
24%&(*;(file1(*'(7=>&4(,3 "+ ( file2<(7 4 (*; (file2 (&9 *,'("+>(
file1(>7&'(+7,B(
(
@(<-+=#6?*5)<-'?+#?5&<)(?<',#
#
string1#AA#string2(( D756"4&(',4*+C'(&$%"=*,8B(
string1#3A#string2# D756"4&(',4*+C'(*+&$%"=*,8B(
(
!<-(%*&(-6#6?*5)<-'?+#?5&<)(?<',#
#
E&+&4"==8( +B*&<-6( #756"4*'7+'( 7+( >7%A=&( '$%"4&(
A4"#F&,'("4&(7A'7=&,&<(37@&G&4(87%(5"8(',*==(%'&>(9&C<(9
+&<( 97(<( 97&<( 9=(<( 7 4( 9=&<( 5&"+ *+C( &C%"=<( +7,( &C%"=<( 7&''(
(3"+<( 7&''( ,3"+( 74( &$%"=<(=4&",& 4( (3"+<( "+>( =4&",&4( (3"+(
74(&$%"=<(4&'6&#,*G&=8B#
2#34#8?<#)<-(%*&(-6(D)7B&'#-(E'# %-=%7F#<&6?**&+:&: #(?#
B'&# (%&# 'F+()G# HH# 6?+:-(-?+# ""B# H+( ,3",( #"'&<( ,3&(
;7==7@*+C(7 6 &4 ", 74'(5%',(A&(%'&>(I(# AA# <#3A<( I( <( IA<(
J#<(JA(
(
K)((&<+#*)(6%-+=#?5&<)(?<,#
(
string#=~#regularExpression# #
24%&(*;(string(5",#3(,3&(6",,&4+(7;(,3&(4&C%="4(
&964&'*7+B(
(
L?=-6)7#?5&<)(?<'#,#
#
-.#3#//#condition#00# #
3##9#JK2(-(J&C",&(,3&(,4%,3B(LG"=%",&("'(,4%&(7+=8(
*;(condition(*'(;"='&B(
-.#//#condition1#00#MM#//#condition2#00#
MM#9#MJ)(N((D3&#F(*;(A7,3(#7+>*,*7+'("4&(,4%&B(
(
-.#//#condition1#00#NN#//#con dition2#00#
NN#9#KO(N(D3&#F(*;(7+&(7;(,3&(#7+>*,*7+'(*'(,4%&B(
(
O"#$%&#case#'()(&*&+(,#
#
6)'&#$variable#-+#
#####pattern1"#######
##########commands1#
##########11#
#####pattern2Npattern3Npattern4"#######
##########commands2#
##########11#
#####patternN"#
##########commands3#
##########11#
#####P"#
##########commands4#
##########11#
&')6#
#
H,("==7@'(,7(#3&#F("(G"=%&(5%=,*6=&(,*5&'B(H;(,3 &($variable(
5",#3( ,3&( pattern1<( ,3&( commands1( "4&( &9&#%,&>B( H;( *,(
5",#3&'(+7+&(7;(,3&5<(,3&(commands4("4&(&9&#%,&>B(
The loops#
(
!"#$%&#for#7??5,#
(
.?<#i#-+#element1#elem ent2#element3#1#:?#
command#
:?+&#
O&6&",(,3&(#755"+>(A8("''*C+*+C(=*',?'(
&=&5&+,'(,7(,3&(G"4*"A=&(iB(2 3 & (7-' (( #" +(A&(
*56=*#*,(P&BC ( *.txt ,3",(*,&4",&'(7G&4("==(,3&(,9,(
;*=&'BQ(
(
.?<#HH#iAQ#1# iIAQR#1#iSS#""#1#:?#
command#
:?+&#
23*'(*'("(D-',8=&(;74(=776(,3",(*,&4",&'(7G&4(,3&(
*+,&C&4'(P3 &4&(;475 (0 (,7(0/(+" 5 &=8(0/ (,*5& 'QB((
.?<#i#-+#T1UU10V1#:?#
command#
:?+&#
23*'('8+,"9("==7@'(,7(*,&4",&("('&=&#,&>(+%5A&4(
7;(,*5&(P3&4&(;475(0(,7(0/(+"5&=8(0/(,*5&'QB(
(
.?<#i#-+#WT3array/X0V#1#:?#
##&6%?#YZ&F#,Y#Wi#
##&6%?#YD)7B&,Y#WTarray/Wi0V#
:?+&#
# H,&4",&(7G&4("+("''7#*",*G&("44"8B(
(
O"#$%&#while#7??5,#
23&(@3*=&(=776(#7+,*+%&((%+,*=(,3&(#7 + >*, *7+ (*' (;" = '&B(
#
iAR#
[%-7&#HH#$i#IA#10#""1#:?#
# 6?**)+:# #
HHiSS""#
:?+&#
H,&4",&(%+,*=($i(*'('%6& 4*7 4 (,7(0 / <(+" 5&=8(0/(
,*5&'B(
(
(
(
(
!"#$%&'()"*+",(-(./01(
File reading
.?<#line#-+#WH6) (#file.txt"1#:?##
command#
:?+&# # #
O&">(,3&(;*=&(file7=*+&(A8(=*+ & ("+>(&9&#%,&(,3&(
#755"+>B(2#3 4#R&4&(,3&(=*+&(*'( > &;*+ & >(A8(,3& (
HST(G"4*"A=&(P'&&('&#,*7+?'(&+ > QB((T&,(*,(,7(UV+U(
,7(7A,"*+(,3&(A&3"G*74(&96 &#,&> B(
#
[%-7&#<&):#line#1:?### # O&">(,3&(;*=&(file((
command### # =*+&(A8(=*+&("+>#
:?+&#I#file# # ############&9&#%,&(,3&(#755"+>B#
(
\8@AW]4+]# T&,(,3&(\+,&4+"=(8*&=>(@&6"4",74(PHSTQ(
G"4*"A=&(,7(UV+UB(W8(>& ;" %=,(*,'(G"= % & (*' (U(V,V+U(
P'6"#&<(,"A("+>(+&@=*+&QB(
(
Arrays and Hashes
!"#\+:&G&:#)<<) F#,#
(
arrayAH"# 74# #:&67)<&#>)#array#
)&#="4&( "+( *+>&9&>( "44"8( "+>( *+*,*"=*X&( *,( ,7(
A&( &56,8B( H+( ,3&( '&#7+>( #"'&( "+( &9*',*+C(
"44"8(*'(+7,(*+*,*"=*X&>B(
(
arrayAH!++)#K)<#^77)"#
23&("44"8(array(*'(*+*,*"=*X&> (@*,3(,34&&(G" =% & ' B(
#
array/N0=value#
T&,(,3&(&=&5&+,(N(7;(,3&("44"8(array(,7(value7
#
array+=(value1#value2#value3)#
M66&+>(,3&("44"8(@*,3(,34&&(G"=%&'B7
WTarray/N0V#
L96"+>(,3 &( &=&5&+,( 4&;&4&+ #&>( A8( ,3&( *+>&9 (
N(;475 (arrayB(
(
WT_)<<)F/N0V#
T*X&( P',4*+C( =&+C,3Q( 7;( ,3& ( G"=%&( 4&;&4&+#&>(
A8(,3&(*+>&9(N(*+ (array(
(
WT_array/X0V#
T*X&(P+%5A&4(7;(&=&5&+,'Q(7;(arrayB(
#
WT#3array/X0V#
L96"+>( &"#3( array( *+>&9( F& 8 ( "'( "( '&6"4",&(
"4C%5&+,B(
(
WT)<<)F/X0V#
L96"+>("==(,3&(G"=%&'(',74&>(*+(arrayB(
(
B+'&(#9D#array/N0#
)&',478(,3&(array(&=&5&+,(",(*+>&9(NB(
#
B+'&(#9D#array#
)&',478(,3&(#756=&,&(arrayB(
((
O"#!''?6-)(-D&#)<<)F#2#34#8<?*#O)'%#`#2#34,#
#
:&67)<&#>!#array#
( )&#="4&("+("''7#*",*G&("44"8(arrayB(
(
array#AH/string10Avalue1#/string20Avalue2"#
M''*C+( ,@7( G"=%&'( *+( "+ ( "''7#*",*G&( arrayB(
Y7%(5%',(>&#="4&(,3&("''7#*",*G&("44"8(;*4',B(
(
K5*,,*+C( ,3&( "66&+>( #75 5 "+>( PZ[Q<( "==( 7,3&4(
#755"+>'( "4&( '*5*="4( ,7( ,37'&( 7;( *+>&9&>( "44"8B(
L9#&6,( ,3",( ,3&( *+>&9( F&8( *'( +7( 574&( "( +%5&4*#"=(
G"=%&(N(A%,("(@$a\bcB(
Programming in bash
_32d-+2d )' % #
\4*,,&+(",(,3&(,76(7;(87%4('#4*6,<(*,("==7@'(,7(>&;*+&(
,3&('3&==(,7(%'&B(K6,*7+(#"+(A&("> >&> ("'(>G(;74(>&A A % CB(
(
'7&&5#eR # T%'6&+>(&9&#%,*7+(;74("+(*+,&4G"=(7;((]/('&#7+>'B(
(
&G-(# ^%*,(,3&(647C4"5B#
#
U2scriptU'%# #L9&#%,&(,3&('#4*6,(script(P23&(;*=&?'(
&9&#%,*+C(4*C3,(*'(+&&>&>QB(
_#comment(( 23*'(*'("(#755&+,B(
!"#!<=B*&+('##
#
U2scriptU'%#arg1#arg2#
\*,3(,3",(#755"+><(,3&(' # 4* 6 , (4& # & *G&(G"=%& '(
'#4*6,B'3(*+(WR<(arg1(*+(WQ<(arg2(*+(WfB(
W_# 23&(+%5A&4(7;("4C%5&+,'B#
WX# M44"8(7;("4C%5&+,'B#
(
O"#8B+6(-?+'#
#
_(`V(M(;%+#,*7+(5%',(A&(>&;*+&>(A&;74&(,7(#"==(*,B(
(
.B+6(-?+#hello#T#
command##
################V#
)&;*+*,*7+(7;("(;%+#,*7+(#"==&>(helloB(23&(command(@*==(
A&(6&4;745&>(@3&+(,3&(;%+#,*7+(*'(#"==&>B(
(
%&77?#arg1#arg2#
D"==( ,3&( ;%+#,*7+( +"5&>( hello( @*,3( .(
"4C%5&+,'B((
(
2#34 #g-(%-+#.B+6(-?+'h#) <=B*&+('#)<&#(<&)(&:#-+#(%&#')*&#
*)++&<#)'#)<=B*&+('#=-D&+#(?#)#'6<-5(U#
(
i"#^'&<#-+(&<.)6&##
<&):#variable#
\"*,(;74("+(%'&4(*+6%,("+>('"G&(,3&(*,(*+(variableB(
(
?5(-?+'AYopt1#opt2Y#
'&7&6(#?5(#-+#W?5(-?+'1#:?#
-.#/#YW?5(Y#A#Yopt1Y#01#(%&+#
# # command1#
&7-.#/#YW?5(Y#A#Yopt2Y#01#(%&+#
command2#
&7'&#
command3#
.-#
:?+&#
23*'( *' ( "( ,&9 , ( A" '& > ( %' &4 - ;4 *&+ > =8( 5&+%B( H,(
64756,'(,3&(%'&4(;74(&"#3(a76,?(*+(b76,*7+'B(