You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
${FOO:?message} Show error message and exitif$FOO is not set
The : is optional (eg, ${FOO=word} works)
#Loops
Basic for loop
foriin /etc/rc.*;do
echo$i
done
Ranges
foriin {1..5};do
echo"Welcome $i"
done
With step size
foriin {5..50..5};do
echo"Welcome $i"
done
Reading lines
< file.txt |whileread line;do
echo$line
done
Forever
whiletrue;do
···
done
#Functions
Defining functions
myfunc() {
echo"hello $1"
}
# Same as above (alternate syntax)
functionmyfunc() {
echo"hello $1"
}
myfunc "John"
Returning values
myfunc() {
local myresult='some value'
echo$myresult
}
result="$(myfunc)"
Raising errors
myfunc() {
return 1
}
if myfunc;then
echo"success"
else
echo"failure"
fi
Arguments
$# Number of arguments
$* All arguments
$@ All arguments, starting from first
$1 First argument
See Special parameters.
#Conditionals
Conditions
Note that [[ is actually a command/program that returns either 0 (true) or 1 (false). Any program that obeys the same logic (like all base utils, such as grep(1) or ping(1)) can be used as condition, see examples.
[[ -z STRING ]] Empty string
[[ -n STRING ]] Not empty string
[[ STRING == STRING ]] Equal
[[ STRING != STRING ]] Not Equal
[[ NUM -eq NUM ]] Equal
[[ NUM -ne NUM ]] Not equal
[[ NUM -lt NUM ]] Less than
[[ NUM -le NUM ]] Less than or equal
[[ NUM -gt NUM ]] Greater than
[[ NUM -ge NUM ]] Greater than or equal
[[ STRING =~ STRING ]] Regexp
(( NUM < NUM )) Numeric conditions
[[ -o noclobber ]] If OPTIONNAME is enabled
[[ ! EXPR ]] Not
[[ X ]] && [[ Y ]] And
[[ X ]] || [[ Y ]] Or
File conditions
[[ -e FILE ]] Exists
[[ -r FILE ]] Readable
[[ -h FILE ]] Symlink
[[ -d FILE ]] Directory
[[ -w FILE ]] Writable
[[ -s FILE ]] Size is > 0 bytes
[[ -f FILE ]] File
[[ -x FILE ]] Executable
[[ FILE1 -nt FILE2 ]] 1 is more recent than 2
[[ FILE1 -ot FILE2 ]] 2 is more recent than 1
[[ FILE1 -ef FILE2 ]] Same files
Example
if ping -c 1 google.com;then
echo"It appears you have a working internet connection"
fi
if grep -q 'foo'~/.bash_history;then
echo"You appear to have typed 'foo' in the past"
fi
# String
if [[ -z"$string" ]];then
echo"String is empty"
elif [[ -n"$string" ]];then
echo"String is not empty"
fi
# Combinations
if [[ X ]] && [[ Y ]];then
...
fi
# Equal
if [[ "$A"=="$B" ]]
# Regex
if [[ "A"=~"." ]]
if(($a<$b));then
echo"$a is smaller than $b"
fi
if [[ -e"file.txt" ]];then
echo"file exists"
fi
#Arrays
Defining arrays
Fruits=('Apple''Banana''Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
Working with arrays
echo${Fruits[0]}# Element #0
echo${Fruits[@]}# All elements, space-separated
echo${#Fruits[@]}# Number of elements
echo${#Fruits}# String length of the 1st element
echo${#Fruits[3]}# String length of the Nth element
echo${Fruits[@]:3:2}# Range (from position 3, length 2)
Operations
Fruits=("${Fruits[@]}""Watermelon") # Push
Fruits+=('Watermelon') # Also Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match