INTRODUCTION TO BASH SCRIPTING
Bash Programming
• Bash Programming Language can perform basic arithmetic operations.
• Create a new file named math.sh in ~/ directory.
• Open the file in your preferred text editor.
• Type the following code into math.sh:
%%sh
#!/usr/bin/env bash
# File: math.sh
expr 5 + 2
expr 5 - 2
expr 5 \* 2
expr 5 / 2
Bash Programming
• Save the file and run the script in your shell:
%%sh
bash math.sh
• Expected Output:
7
3
10
2
• Explanation:
• Bash executes programs line by line.
• expr evaluates Bash expressions.
• Arithmetic operators (+, -, *, /) work as expected.
• Use \* to escape the multiplication operator.
Variables
• Variables in Bash can store data.
• Naming conventions for variables:
• Lowercase letters.
• Start with a letter.
• Alphanumeric characters and underscores.
• Words separated by underscores.
1
• Example of variable assignment:
%%sh
chapter_number=5
Variables
• Print the value of a variable:
%%sh
echo $chapter_number
• Modify variable value using arithmetic operators:
%%sh
let chapter_number=$chapter_number+1
• Variables can store strings:
%%sh
the_empire_state="New York"
• Command substitution:
%%sh
math_lines=$(cat math.sh | wc -l)
echo $math_lines
• Free variables in Bash scripts:
• $@: Array of script arguments.
• $1, $2, …: Individual arguments.
• #: Total number of arguments.
• Example script using script arguments:
%%sh
#!/usr/bin/env bash
# File: vars.sh
echo "Script arguments: $@"
echo "First arg: $1. Second arg: $2."
echo "Number of arguments: $#"
# usage: sh var.sh 1 2 4 5
User Input
• read command: Accept user input in scripts.
• Example script using read:
2
%%sh
#!/usr/bin/env bash
# File: letsread.sh
echo "Type in a string and then press Enter:"
read response
echo "You entered: $response"
Logic and If/Else
• Logic operations and conditional statements in Bash.
• Example of if/else statement:
%%sh
if [ condition ]; then
# code block
else
# code block
fi
• Example script using if/else:
%%sh
#!/usr/bin/env bash
# File: ifelse.sh
echo "Enter a number:"
read num
if [ $num -gt 10 ]; then
echo "Number is greater than 10."
else
echo "Number is not greater than 10."
fi
Logic and If/Else
In Bash, there are six relational operators for number comparison:
• -eq: Checks if two numbers are equal
• -ne: Checks if two numbers are not equal
• -gt: Checks if the first number is greater than the second number
• -lt: Checks if the first number is less than the second number
• -ge: Checks if the first number is greater than or equal to the second number
• -le: Checks if the first number is less than or equal to the second number
Comparing Multiple Numbers
• Comparing more than two numbers in Bash involves nested if statements or
using logical operators like && (AND) and || (OR).
3
• Let’s look at an example where we compare three numbers to find the largest:
%%sh
#!/usr/bin bash
num1=7
num2=12
num3=9
if [ "$num1" -ge "$num2" ] && [ "$num1" -ge "$num3" ]; then
echo "$num1 is the largest number"
elif [ "$num2" -ge "$num1" ] && [ "$num2" -ge "$num3" ]; then
echo "$num2 is the largest number"
else
echo "$num3 is the largest number"
fi
References
• https://seankross.com/the-unix-workbench/