Bash Script Flashcards
What is set command?
set returns all environment variables + extras like functions etc which env would not return
What is $#?
Number of all parameters
What is $0, $1
Program name, first argument
What is $*
All arguments
How to print parameter 12?
${12}
What does this do?
var=”one two three four five”
set – $var
sets the input parameters as one two three four five
for i do echo $i done
when range is neglected the for loop loops through the input parameters so $i is $1, $2 etc
Difference between echo $varname and echo “$varname”?
Same if there are no multiple white spaces. In case there are, $varname will print only 1 space while “$varname” will print all the spaces.
hello=”K L M”
1. Output: echo “$hello”
2. Output: echo $hello
3. Output: echo ‘$hello’
- K L M
- K L M
- $hello
IFS=’!’ read a b
echo a is $a
echo b is $b
- input = kostas mylonas
- input = kostas!mylonas
- a is kostas mylonas
b is
2.
a is kostas
b is mylonas
Default IFS is whitespace
str=”kostas”
echo ${str^^}
KOSTAS
str=”KOSTAS”
echo ${str,,}
kostas
str=”KOStas”
echo ${str,}
kOStas
str=”kOstAs”
echo ${str^}
KOstAs
anArray=(‘el1’ ‘el2’ ‘el3’)
1. echo ${anArray[@]}
2. echo ${!anArray[@]}
3. echo ${#anArray[@]}
- whole array: el1 el2 el3
- array indices: 0 1 2
- size of array: 3