What is Bash? Bash (Bourne Again SHell) is a shell and programming language for the GNU Operating System. It's widely used on various UNIX-like systems and is the default shell on most Linux distributions and macOS.
Creating a Bash Script To start writing a Bash script, create a new text file and start with the shebang line, which tells the system which interpreter to use:
#!/bin/bash
Make the script executable:
chmod +x script_name.sh
Run the script:
./script_name.sh
Echo Command Used to display a line of text/string:
echo "Hello, World!"
Variables Creating and using variables:
name="Alice"
echo "Hello, $name!"
Reading Input Reading input from the user:
read -p "Enter your name: " name
echo "Hello, $name!"
If-Else Statement Simple if-else example:
read -p "Enter a number: " num
if [[ $num -gt 10 ]]
then
echo "The number is greater than 10."
else
echo "The number is 10 or less."
fi
For Loop Loop through a list of numbers:
for i in {1..5}
do
echo "Number $i"
done
While Loop Condition-based looping:
count=1
while [[ $count -le 5 ]]
do
echo "Count $count"
((count++))
done
Handling multiple conditions using a case statement:
read -p "Enter your choice: " choice
case $choice in
1) echo "You chose option 1";;
2) echo "You chose option 2";;
*) echo "Invalid option";;
esac
Creating Functions A simple function to display a greeting:
function greet {
echo "Hello, $1!"
}
greet "Alice"
Return Values Using output from a function:
function add {
local sum=$(( $1 + $2 ))
echo $sum
}
result=$(add 5 3)
echo "The sum is $result"
Debugging Turn on debugging:
set -x
Turn off debugging:
set +x
Arrays and Substrings Working with arrays and manipulating their content:
arr=(apple banana cherry)
echo "${arr[1]}" # Outputs banana
echo "${arr[@]:1:2}" # Outputs banana cherry
Script with Arguments Using positional parameters:
#!/bin/bash
echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
Run this script with two arguments to see how it handles input parameters.
This tutorial covers the basics and some advanced aspects of Bash scripting. With practice, you'll get more comfortable and discover even more functionalities of Bash.