pwd
whoami
pwd - shows current present working directory
whoami - Shows the current use
Declaring Variables:
#!/usr/bin/env bash
NAME="John"
echo "Hello $NAME!"
echo $NAME
echo "$NAME"
echo "${NAME}!"
Functions :
# One Way
myfunc() {
echo "hello $1"
}
# Second Way
function myfunc() {
echo "hello $1"
}
# Calling it
myfunc "John"
# Declaring local variables in Functions
myfunc() {
local myresult='some value'
echo $myresult
}
Raise Errors :
myfunc() {
return 1
}
if myfunc; then
echo "success"
else
echo "failure"
fi
Defining Arrays :
Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
Operations on these Arrays :
Fruits=("${Fruits[@]}" "Watermelon") # Push
Fruits+=('Watermelon') # Also Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match
unset Fruits[2] # Remove one item
Fruits=("${Fruits[@]}") # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file
echo ${Fruits[0]} # Element #0
echo ${Fruits[-1]} # Last element
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)
echo ${!Fruits[@]} # Keys of all elements, space-separated
Iteration over an Array :