Bash Script Learnings
These are some of my notes from my time spent learning Bash script. Read and be blessed.
- Print statement: echo “Hello, World“
- Functions in bash:
- Bash functions don’t take input parameters like functions in languages like JS, this might throw you off a bit. Instead, it saves the inputs used when calling the function and puts them in variables starting with a dollar sign and the position of the argument for instance, “$1“ -> this means it’s the first parameter supplied to the function.
// function signature (with and without an input parameter)
greeting () {
echo "Yoo $1"
}
Assuming we call the above function like so
greeting “Chi“
The string “Yoo Chi“ will be printed on the screen.
-
To check if an input argument exists when calling a bash script
if [ $# -eq 0 ] <---- $# returns the number of input arguments passed to the script then else fi
$# returns the number of input arguments passed to the script.
-eq 0 evaluates if the input arguments are equal to 0.
- Exit status: If a command exits with 0, it is a success. If it exists, with anything other than a 0, it is a failure.
Powered By Swish