Bash scripting also known as shell scripting is nothing but a programming language for operating machines in Unix or Unix-like systems such as Linux. There are a huge number of already written scripts that automate the repetitive tasks which we call commands. Moreover, it allows user to write their own script and interact with the system resources as per needs. This article provides few Bash script examples that aims to teach you to write Bash script from scratch.
Bash scripts are plain text files with the “.sh” extension, and they can be executed by invoking the Bash interpreter. A typical Bash script starts with a shebang (#!/bin/bash), which indicates the directory or path to the Bash interpreter. After the shebang, one can define variables and functions, use conditionals and loops and execute built-in commands like any other programming language.
Key Takeaways
- Writing your own script and executing it.
- Basics of scripting.
- Advance Scripting idea.
- Error Handling and Debugging in Bash.
Free Downloads
Where to Write a Bash Script
Bash scripts are essentially text files with .sh extension. You can write a Bash script in any text editor. However, to execute the script you need a Unix or Unix-like operating system where Bash interpreter is available.
How to Execute a Bash Script
To successfully execute a Bash script, the first step is to ensure the script has the necessary execution permissions. Once the permissions are set, you can execute the script by calling it from the terminal.
28 Most Useful Bash Scripting Examples
The primary objective of Bash scripting examples is to provide you with a fundamental understanding of bash scripting. Without further delay, let’s jump straight into our first example.
Example 1: Starts with Hellow World
In this example, I am going to write a Bash script that can print “hello world”. The purpose of this script is to show you how echo command works in Bash.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in the built-in nano editor:
nano hello.sh
- nano: Opens a file in the Nano text editor.
- sh: Name of the file.
❸ Copy the following script and paste it into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.
#!/bin/bash
echo “Hello World”
#!/bin/bash is the shebang or hashbang. This line tells the system that this script should be run using the Bash shell. The echo command is used to print or display messages to the console. In this script, it is used to output the string “Hello World“.
❹ Use the following command to make the file executable:
chmod u+x hello.sh
- chmod: Changes permissions.
- u+x: Giving the owner executing permission.
- sh: Name of the script.
./hello.sh
When executed the script prints Hello World in the terminal as shown in the above image.
Example 2: Defining Variables in a Bash Script
One can assign values to a variable using equal sign (=) in Bash. Furthermore, print the assigned values using echo $VARIABLE_NAME. Moreover, this example will give you a clear idea of why quotations can be useful in certain situations in echo command though it’s not mandatory.
Scripts (variable.sh) >
#!/bin/bash
name=Tom
age=12
echo "$name's age is $age"
The provided Bash script assigns the values “Tom” to the name variable and 12 to the age variable. It then uses the echo command to display the message “Tom’s age is 12” on the terminal.
Run the script by the following command:
./variable.sh
The script prints “Tom’s age is 12” by incorporating the values of the variables’ name and age using variable expansion by dollar sign ($).
- Use the equal sign (=) to assign values to variable names.
- To refer to a variable use the dollar sign ($) e. $VARIABLE_NAME
- Variable names are case sensitive i.e. ‘A’ and ‘a’ are different.
- While updating/changing the variable values use only the variable name with the assignment operator(=) i.e. VARIABLE_NAME= NEW_VALUE.
- No need to define variable type while declaring variables.
- Enclose multiple words or string values within Single Quote (” ) or Double Qoute (“”) to consider all characters as input.
Example 3: Environment Variables in Bash
There are a few environment variables that contain information about the environment in which the shell is running. These variables are special and are accessible to all scripts within the environment.
Scripts (env_var.sh) >
#!/bin/bash
echo $HOME # Prints the home directory of the current user
echo $PATH # Prints the directories in which the shell searches for executable files
In the above script, basename is used to extract the base name of the script using the $0 variable. The resulting name is then stored in the script_name variable and later shown using the echo command.
Run the script by the following command:
/env_var.sh
Once you execute the above script, the values of the variables HOME and PATH will be displayed, as shown in the output image.
The list below contains the most common environment variables.
- HOME: The home directory of the current user.
- USER: The username of the current user.
- PWD: The current working directory.
- PATH: A colon-separated list of directories in which the shell looks for executable files.
- SHELL: The path to the current shell executable.
Example 4: Delete or Unset Variable
User can delete or unset a variable using the unset command in Bash scripting. Unsetting a variable makes the variable undefined or empty.
Scripts (del_var.sh) >
#!/bin/bash
name="LinuxSimply"
echo "Name before deletion: $name"
unset name
echo "Name after deletion: $name"
The given Bash script starts by assigning the value “LinuxSimply” to the variable name. It then prints the value of name before deletion, which is “LinuxSimply“. The unset command is used to remove the variable name from the shell environment. After unsetting the variable, it attempts to print the value of name again, resulting in an empty line since the variable no longer exists.
Run the script by the following command:
/del_var.sh
After unsetting the variable name, the echo command can’t retrieve its value as earlier.
Example 5: Getting the Script Name
One can easily find the name of a Bash script using the basename command. Here, $0 variable contains the script name. Let’s say we have a script called name.sh. Now, we want to return back the script name of the script itself.
Scripts (name.sh) >
#!/bin/bash
script_name=$(basename "$0")
echo "The script name is: $script_name"
In the above script, basename is used to extract script name using the $0 variable. The resulting name is then stored in the script_name variable. Then it is displayed using the echo command.
Run the script by the following command:
./name.sh
If you execute the program it will print the name of the script as you can see in the image above.
Example 6: Taking User Input in a Variable
The read command is used to take user input. Once you use -p the command is enabled to prompt a message to user along with taking input. Later, you can use echo $VARIABLE_NAME to display the user input on the screen.
Scripts (input.sh) >
#!/bin/bash
read -p "Enter a number:" num
echo "You entered: $num"
The given Bash script prompts the user to enter a number using the read command with the -p option. The user’s input is stored in the num variable. Then, the script uses echo to print “You entered: ” followed by the value stored in num. Then the echo command is used to display the number
Run the script by the following command:
./input.sh
Here 25 is the user input that is saved in the num variable. Later the program recalls the user input using the echo command.
Example 7: Display Output in Terminal or Saving in a File
As of now you can take input from the user and save it in variable. Now I am going to demonstrate how to perform an arithmetic operation using variables and display the resulting output in the terminal or save it in a file. To perform any arithmetic calculation in Bash use $(()) syntax. See the script below for more clarification.
Scripts (output.sh) >
#!/bin/bash
read -p "Enter a number: " num1
read -p "Enter another number: " num2
add=$((num1 + num2))
echo "Addition of numbers: $add"
The provided script allows users to input two numbers and calculates their sum. When executed, it prompts the user to enter a number, which is stored in the variable num1. It then requests another number, which is stored in the variable num2. The script proceeds to add num1 and num2 together using the arithmetic expansion syntax $(()), assigning the result to the variable add. Finally, it uses the echo command to display the message “Addition of numbers: ” followed by the value of add.
./output.sh
The first input given by the user is 5 and the second input is 10. The program adds both numbers and prints the result of the addition 15 in the terminal.
Saving Output in a File
You may want to write the output of the program in a file instead of displaying it in the terminal. Here is how you can redirect the output to a file.
Scripts (output1.sh) >
#!/bin/bash
read -p "Enter a number: " num1
read -p "Enter another number: " num2
add=$((num1 + num2))
echo "Addition of numbers: $add" > output.txt
The provided script allows users to input two numbers and calculates their sum. When executed, it prompts the user to enter a number, which is stored in the variable num1. It then requests another number, which is stored in the variable num2. The script proceeds to add num1 and num2 together using the arithmetic expansion syntax $(()), assigning the result to the variable add. Instead of displaying the output in the terminal, it writes the output in the output.txt file.
Run the script by the following command:
./output1.sh
Here the first input from the user is 40 and the second input from the user is 10. But the result of addition is not shown in the terminal. To see what is written in the output.txt file run the following command in the terminal as shown in the image.
cat output.txt
Once you run the command you can see “Addition of numbers :50” is written in the text file.
Syntax of Redirecting Output
Redirection | Syntax |
---|---|
Output Redirection | > |
Error Redirection | 2> |
Output and error | &> or 2>&1 |
Appending with previous output | >> |
Example 8: Calculation of Floating Point Using Pipe
Bash shell primarily performs integer arithmetic. Hence arithmetic calculation on floating point numbers can be challenging. You need to use pipe (|) to perform arithmetic calculations on floating point numbers. It’s a mechanism to connect the output of one command as an input of another command
Scripts (float.sh) >
#!/bin/bash
result=$(echo "3.14 + 2.7" | bc)
echo "$result"
The expression 3.14 + 2.7 is echoed and then passed as input through the pipe to the bc command for arithmetic evaluation. The command substitution, indicated by $(…), captures the output of bc and assigns it to the result variable. Lastly, the value of the result is printed to display the arithmetic evaluation.
Run the script by the following command:
./float.sh
Once executed the program prints the summation of two floating point numbers 3.14 and 2.7.
Example 9: Bash If Conditionals
In Bash, the if statement is used to check conditions. The statement starts with the word if and ends with the word fi. One can check single or multiple conditions using only one if statement. Look at the following script for better visualization.
Scripts (if_statement.sh) >
#!/bin/bash
# Checking if a number is positive or negative
read -p "Enter a number:" num
if [ $num -gt 0 ]; then
echo "The number is positive."
else
echo "The number is negative or zero."
fi
The condition [ $num -gt 0 ] checks if the value of the variable num is greater than zero. If the condition is true, the code within the then block is executed, which in this case prints “The number is positive.” If the condition is false, the code within the else block is executed, which prints “The number is negative or zero.“
Run the script by the following command:
./if_statement.sh
The above image shows that when user input is -5, the program executes the code within the else block as the condition is not true.
Below is a list of comparison operators that can be useful for evaluating conditions using if statement.
- -eq: equal to
- -ne: not equal to
- -lt: less than
- -gt: greater than
- -le: less than or equal to
- -ge: greater than or equal to
Example 10: Or/And Condition in IF Statement
Sometimes user may check multiple conditions in a single if statement. Logical operators are quite helpful for this purpose.
Scripts (and_or.sh) >
#!/bin/bash
read -p "Enter your age:" age
read -p "Enter marks obtained:" marks
if [ $age -gt 25 ] && [ $marks -gt 800 ]; then
echo "Congratulations! You are eligible for a scholarship."
elif [ $age -gt 25 ] || [ $marks -gt 800 ]; then
echo "Submit administrative reference for the scholarship."
elif [ $age -lt 25 ] && [ $marks -lt 800 ]; then
echo "Sorry, you are not eligible for the scholarship."
fi
Above script determines scholarship eligibility based on two variables age and marks. If the age is greater than 25 and(&&) the marks is greater than 800, it prints “Congratulations! You are eligible for a scholarship.“
If the age is greater than 25 or(||) the marks is greater than 800, it prints “Submit administrative reference for the scholarship.“
If the age is less than 25 and the marks is less than 800, it prints “Sorry, you are not eligible for the scholarship.“
./and_or.sh
In the above output, with an age of 22 and marks of 780, only condition under the second elif holds true and the script outputs “Sorry, you are not eligible for the scholarship.”
Logical operators of Bash script:
- &&: AND operator
- ||: OR operator
- !: NOT operator
Example 11: Checking Empty Variable
One can check whether a variable is empty or not using a simple Bash program below.
Scripts (empty_var.sh) >
#!/bin/bash
var=""
if [ -z "$var" ]; then
echo "The variable is empty."
else
echo "The variable is not empty."
fi
The script checks whether the variable named var is empty or not using an if statement in Bash. The -z operator within the condition tests if the length of the variable is zero, indicating that it is empty.
If the variable is empty, the script executes code under if block and prints the message “The variable is empty.” Otherwise, it executes the code within the else block.
Run the script by the following command:
./empty_var.sh
As the variable is empty, the program executes the if block to echo the proper message, as highlighted in the image above.
Example 12: Bash Case Statement
Sometimes Bash case statement can be a useful alternative to the if-else statement. It looks for patterns and performs specific actions based on the matched patterns. The case statement starts with the word case and ends with the word esac.
Scripts (case.sh) >
#!/bin/bash
read -p "Enter file extension: " extension
case $extension in
".txt"|"log")
echo "It's a text file or a log file!"
;;
".csv"|".xlsx")
echo "It's a spreadsheet file!"
;;
esac
The given Bash script prompts the user to enter a file extension and uses a case statement to determine the type of file based on the extension. Then it reads the user’s input and stores it in the extension variable.
The case statement evaluates the value of extension and checks for specific patterns. If the value matches either “.txt” or “log“, it executes the corresponding code block, displaying the message “It’s a text file or a log file!” on the terminal.
Similarly, if the value matches either “.csv” or “.xlsx“, it executes the relevant code block, printing “It’s a spreadsheet file!” as the output.
Run the script by the following command:
./case.sh
User inputs log as the file extension. It matches the first pattern of the case statement and hence displays the message shown in the above image.
Example 13: For Loop in Bash
Every programming language offers some control structure for looping over an iterable. Bash for loop is a fundamental construct in shell scripting for this purpose. The script below shows the use of for loop for counting files of the directory.
Scripts (for_loop.sh) >
#!/bin/bash
count=0
for file in *.txt
do
echo "$file"
count=$((count+1))
done
echo "Total number of files in the current directory: $count"
The variable count is initialized to 0, representing the text file count. The for loop iterates over each file with the .txt extension using the pattern *.txt.
Within the loop, the count variable is incremented by 1 for each file encountered and print the name of the matching file as well. After the loop completes, the total number of txt files in the current directory is displayed using the echo command.
Run the script by the following command:
./for_loop.sh
The image shows output1.txt and output.txt are two existing txt files in the current directory.
Example 14: While Loop in Bash
The while loop is another important control structure in Bash scripting. Hopefully, you will get a clear idea about the while loop from the following script.
Scripts (while_loop.sh) >
input=""
read -p "Enter q to quit: " input
while [ "$input" != "q" ] || [ "$input" != "Q" ]
do
echo "You entered $input. Press q to quit:"
read input done echo "Program Terminated..."
The provided code enables the user to input values until they enter either “q” or “Q” to quit the program. It begins by initializing the variable input as an empty string.
Then the read command prompts the user to enter a value and assigns it to input. The while loop evaluates whether input is not equal to “q” or not equal to “Q“. If user’s input is not q or Q, it prompts a message of pressing “q” to quit. The read command captures the user’s next input, updating the value of input for the next iteration.
When the user finally enters “q” or “Q“, the loop terminates, and the message “Program Terminated…” is displayed.
Run the script by the following command:
./while_loop.sh
Here, you can see the user first input is hello. As a result, while loop continues and takes another input as world. However, none of them can terminate the program. Finally, the user inputs q. Hence the program terminates immediately.
Example 15: Until Loop in Bash
There is another loop construction in Bash which is called the until loop. The primary purpose of using an until loop is to handle situations where you want to repeat an action until a desired state or condition is achieved.
Scripts (until_loop.sh) >
#!/bin/bash
# Check if a network service is available
until nc -z 127.0.0.1 8080; do
echo "Service is not available. Waiting for it to become accessible..."
sleep 10
done
echo "Service is now accessible."
The provided code uses an until loop to check the availability of a network service. It attempts to establish a connection to 127.0.0.1 (localhost) on port 8080 using the nc command. If the connection fails, indicating that the service is not available, it displays a message stating so. Then it waits for 10 seconds before trying again using sleep. The loop continues until the connection is successfully established. Once the service is accessible, the until loop terminates.
Run the script by the following command:
./until_loop.sh
You can see the loop continues to print a message stating that the service is not available. And after each iteration, it waits for 10 seconds and rechecks the service availability again. The program only terminates if it can successfully establish a connection with the localhost. To deliberately stop the program in the terminal press CTRL+C.
Differences Between the while loop and until loop
While loop | Until loop |
---|---|
The while loop executes until a zero status is returned. | The until loop executes until a nonzero status is returned. |
While loop may execute single or multiple time as long as the condition meets. | The until loop always executes at least once |
Example 16: Select Structure in Bash
Bash provides another loop like structure called select. This loop offers a convenient way to create an interactive menu where user can select one or more options before quitting.
Scripts (select.sh) >
#!/bin/bash
# Declare an array of options
options=("Option 1" "Option 2" "Option 3" "Quit")
# Display the menu and prompt the user for a choice
select choice in "${options[@]}"; do
case $choice in
"Option 1")
echo "You selected Option 1"
# Perform actions for Option 1
;;
"Option 2")
echo "You selected Option 2"
# Perform actions for Option 2
;;
"Option 3")
echo "You selected Option 3"
# Perform actions for Option 3
;;
"Quit")
echo "Exiting..."
break
;;
*)
echo "Invalid option. Please try again."
;;
esac
done
This Bash script demonstrates the use of a select loop to create a menu-driven interface. The array options holds the available menu options. The select loop displays the menu options and prompts the user for a choice. Based on the selected option, the corresponding code block is executed, performing specific actions. If the user selects “Quit,” the script displays an exit message and breaks out of the loop. For any invalid choice, an error message is displayed.
Run the script by the following command:
./select.sh
Here user first inputs r, which is an invalid option. Later Option 1 is chosen. Finally, user quits the program by pressing 4.
Example 17: Loop with Break or Continue
Bash offers continue and break statement to skip or terminate program. This gives the flexibility to control the flow of the loops in an efficient manner.
Scripts (control.sh) >
#!/bin/bash
read -s -p "Enter the password: " password
while true; do
if [ "$password" != "admin123" ]; then
echo "Access denied. Please enter again:"
read password
continue
else
echo "Access granted. Welcome!"
fi
break
done
The script starts by asking the user to input a password and stores it in the password variable. It then enters a while loop that continues indefinitely until the correct password is entered. Inside the loop, an if statement compares the entered password with “admin123“. If the passwords do not match, an “Access denied” message is displayed, and the user is prompted to enter the password again. The continue statement is used to skip the rest of the current iteration of the while loop.The script hides the first input from the user using the -s option with the read command.
Once the correct password is entered, an “Access granted. Welcome!” message is displayed, and the loop is terminated by using the break statement.
Run the script by the following command:
./control.sh
Here the first input is a wrong password. However, user can’t see the input as it is hided using -s option of read command. User gives the second input which is also wrong. But this time it shows the input as the read command within the loop doesn’t include the -s option. Finally, the user gives the correct password. So the program terminates as it reach to the break statement.
Example 18: String Operation in Bash
Scriptwriters can perform various operations on a string in Bash. The script below showcases concatenation, length calculation and substring extraction in Bash.
Scripts (string.sh) >
#!/bin/bash
string1="Linux"
string2="Simply"
result="$string1$string2" # Concatenating with a without delimiter
echo "$result"
length=${#result} # Length of the string
echo $length
substring=${result:5:6}
echo $substring
The provided Bash code demonstrates string manipulation operations. It starts by defining two variables, string1 and string2, with the values “Linux” and “Simply” respectively. The result variable is then assigned the concatenated value of string1 and string2 without any delimiter, resulting in “LinuxSimply“. The echo command is used to print the value of result.
Next, the length of the result string is calculated using ${#result} syntax, and the value is stored in the length variable.
Lastly, a substring is extracted from the result string, starting from index 6 and having a length of 5 characters. This substring, “Simply“, is stored in the substring variable. manipulation.
Run the script by the following command:
./string.sh
For the two predefined strings “Linux” and “Simply” the concatenated string is LinuxSimply. The length of LinuxSimply is 11. And substring from position 5 to the next 6 characters is Simply.
Example 19: Iterating Over Bash Array
There are mainly two types of array in Bash. They are indexed array and associative array. Indexed array can be easily accessed using the index of each item. In the following script I will show you how to iterate over a indexed array.
Scripts (array.sh) >
#!/bin/bash
# Declare an indexed array
myArray=("Apple" 100 "Orange" 50)
# Iterate over the array using a for loop
for item in "${myArray[@]}"; do
echo "$item"
done
An indexed array named myArray is declared and initialized with four elements. The elements consist of a mix of strings (“Apple” and “Orange”) and numbers (100 and 50) each separated by whitespace.
Then for loop is used to iterate through each element of the array. Within the loop, the value of item is displayed using the echo command.
Run the script by the following command:
./array.sh
Once executed the program prints all the items of myArray in a new line.
Example 20: Associative Array in Bash
In Bash, there is a built-in data structure called an associative array, which functions similarly to a Python Dictionary. To create an associative array, you can use the -A option with the declare command.
Scripts (associate.sh) >
#!/bin/bash
# Declare and initialize an associative array
declare -A colors=(
["red"]="#FF0000"
["green"]="#00FF00"
["blue"]="#0000FF"
)
# Print the values using the keys
echo "Color code for red is: ${colors["red"]}"
An associative array named colors is declared and initialized using the declare command. The array is created with three key-value pairs, where the keys are color names (“red“, “green“, “blue“) and the values are their corresponding color codes (“#FF0000“, “#00FF00“, “#0000FF“)
Then the script uses the echo command to print the value associated with the key “red” in the colors array.
Run the script by the following command:
./associate.sh
When executed the program access the value of the “red” key by using ${colors[“red”]} and display it with the echo command.
Example 21: Getting Bash Array by Changing IFS Value
One can change the delimiter in the Internal Field Separator (IFS) variable and obtain a comma-separated array. Here is how you can change the value of the IFS variable.
Scripts (convert_array.sh) >
#!/bin/bash
# Read the comma-separated values from input.txt into a variable
input=$(cat input.txt)
# Set the Internal Field Separator (IFS) to comma (',') to split the values
IFS=',' read -ra array <<< "$input"
# Display the full array
echo "Full Array: ${array[*]}"
This Bash script reads the comma-separated values from input.txt and stores them in the input variable. It then sets the Internal Field Separator (IFS) to a comma (‘,‘) using IFS=’,’. This allows the script to split the values based on the comma delimiter and assign them to the array variable using the read command. Finally, the script displays the full array using ${array[*]}.
Run the script by the following command:
./convert_array.sh
Here fruit names are separated by a comma. The program read names from the input.txt file and created a space separated array as displayed.
Example 22: Creating Bash Function
Functions play a fundamental role in building modular and reusable programs. The following example demonstrates the creation of a simple Bash script function.
Scripts (function.sh) >
#!/bin/bash
# Function definition
greet() {
read -p "Enter your name:" name
echo "Hello, $name! Welcome to Linuxsimply!!"
}
# Function call with an argument
greet
This function allows the user to input their name and receive a personalized greeting message.
The code defines a function named greet in Bash scripting. When the function is called, it prompts the user to enter their name using the read command. The entered name is stored in the name variable. Then, the function outputs a greeting message that includes the entered name using the echo command. The message welcomes the user to “Linuxsimply“.
Run the script by the following command:
./function.sh
Here user put the name Alina. The program gives a welcome message to Alina as shown in the image.
Example 23: Functions with Argument
Arguments in functions are accessed using special variables. The first argument is represented by $1, the second argument by $2 and so on. You can reference these variables within the function to access the values passed as arguments during the function call.
Scripts (factorial.sh) >
#!/bin/bash
# Function to calculate factorial
calculate_factorial() {
local num=$1
local factorial=1
for ((i=1; i<=num; i++)); do
factorial=$((factorial * i))
done
echo "The factorial of $num is: $factorial"
}
# Prompt the user to enter a number
read -p "Enter a number: " number
# Call the function to calculate factorial
calculate_factorial $number
The provided Bash script calculates the factorial of a given number using a function called calculate_factorial().
First, the script defines the function calculate_factorial() that takes an argument num. Inside the function, a local variable factorial is initialized to 1. Then, a for loop runs from 1 to num, incrementing the loop counter i each time. Within the loop, the factorial variable is updated by multiplying its current value with i.
Once the loop completes, the function outputs the calculated factorial with a message indicating the original number. The script prompts the user to enter a number using the read command, and the entered number is stored in the number variable. Finally, the calculate_factorial function is called with the number variable as an argument to compute and display the factorial of the input number.
Run the script by the following command:
./factorial.sh
Here user wants to get the factorial of 5. 120 is shown as the factorial of the given number in the next line.
Example 24: Get Date in Bash
In this example, I am going to run a Bash script that will ultimately print the date in YY-MM-DD format.
Scripts (format2.sh) >
#!/bin/bash
today=$(date +%y-%m-%d)
echo $today
The script utilizes the format “%y” to retrieve year (YY), “%m” for the month (MM) and “%d” for the day (DD). After substituting the date command in the variable “today“, it outputs the value of the variable using the echo command.
Run the script by the following command:
./format2.sh
The program prints the current date 23-07-11 as specified by the format specifiers.
Read more about getting date in Bash from the following articles:
Example 25: Send Mail in Bash
You are on the final line. Hopefully, now you can send email using the mailx command. Now, I am going to write the Bash script to send emails.
Scripts (mailx.sh) >
#!/bin/bash
recipient="[email protected]"
subject="Leave"
body="I would like to request you for next two days leave.\\n
Regards
Laku"
echo -E "$body" | mailx -s "$subject" "$recipient"
The provided Bash script demonstrates a simple example of sending an email using the mailx command. The script sets the recipient’s email address, subject and body of the email. Then, it pipes the body content to the mailx command with the specified subject and recipient address.
Run the script by the following command:
./mailx.sh
When executed, this script will send an email to the recipient ([email protected]) with the subject “Leave” and the given body text.
Example 26: Calling Another Script
For this purpose, you can use the bash command or add the path of the calling script in the PATH variable. I am going to show you both processes.
Scripts (diff_process1.sh) >
#!/bin/bash
# Call and run another script in a different process
echo "This program will give the multiplication of two numbers."
bash ./diff_process2.sh
Scripts (diff_process2.sh) >
#!/bin/bash
read -p "Please enter a number: " number1
read -p "Please enter another number: " number2
result=$((number1 * number2))
echo "The multiplication of the numbers is: $result"
The code includes two scripts diff_process1.sh and diff_process2.sh. diff_process1.sh initiates the execution of diff_process2.sh in a separate process using the bash command. Before executing diff_process2.sh, it displays a message informing the user that the program will calculate the product of two numbers using the echo command.
In diff_process2.sh, the user is prompted to input two numbers, which are stored in the variables number1 and number2. The script then performs the multiplication and displays the calculated multiplication result to the user.
Run the main script by the following command:
./diff_process1.sh
Here, the main script displays a message informing you that the program will calculate the product of two numbers. After that, it starts to execute the diff_process2.sh file and prompts the user to input numbers. The user first inputs 10 and then 5. Then the program displays the multiplication result 50 as you can see in the image.
Example 27: Get Ip Address in Bash
Using the ifconfiq command in association with other commands can extract the IP addresses of a device. Look at the following script for a better understanding.
Scripts (ifcon.sh) >
#!/bin/bash
ip_address=$(ifconfig | grep 'inet ' | awk '{print $2}')
echo $ip_address
The provided Bash script retrieves and displays the IP address of a specified network interface. By default, it finds both loopback and private IP of the device. The script uses the ifconfig command and to gather interface information and then employs awk to filter and extract the line containing the IP address. Finally, it echoes the interface name with the corresponding IP address.
Run the script by the following command:
./ifcon.sh
The program gives IP addresses IP 192.168.0.123 and 127.0.0.1. The IP address 192.168.0.123 refers to the private IP of the device. And 127.0.0.1 refers to the loopback IP of the current device itself.
Example 28: Error Handling by Bash Exit Status
The set -e command is commonly used in Bash scripting for error handling. It stops the script’s execution when it encounters a command with a non-zero exit status, indicating an error.A command returns an exit status of 0 when it executes successfully, while a non-zero exit status is returned when a command fails to run.
Scripts (error.sh) >
#!/bin/bash
set -e
echo "This command will execute successfully."
ls -l
echo "This command will fail, and the script will terminate."
nonexisting_command
echo "This command will not be reached due to the previous error."
# Continue with the script if the command succeeded
echo "Command executed successfully."
The set -e command enables the script to exit immediately if any command returns a non-zero exit status, indicating an error. In this script, the ls -l command is executed successfully and lists the files and directories. However, the subsequent command nonexisting_command is not a valid command, resulting in an error. Due to set -e is enabled, the script terminates at this point without executing any following command. Hence, it will not reach the final echo statement.
Run the script by the following command:
./error.sh
The script perfectly executes the ls -l command but it immediately terminates when it finds nonexisting_command which is not available in Bash.
Conclusion
To summarize, this article covers a wide range of Bash scripting examples, from basic to advanced. Some examples are short and straightforward, while others are more complex and intricate. The goal is to provide you with valuable insights into the world of Bash scripting. I hope you have enjoyed reading this article and find the examples useful in understanding Bash scripting.
People Also Ask
Related Articles
- How to Get Date in Bash [2 Methods with Examples]
- How to Print Time in Bash [2 Quick Methods]
- How to List Users in Bash [2 Easy Ways]
- How to Get Current Time in Bash [4 Practical Cases]
- How to Use Date Format in Bash [5 Examples]
- How to Get Timestamp in Bash [2 Practical Cases]
- How to Copy and Paste in Bash [2 Methods & Cases]
- How to Read Password in Bash [3 Practical Cases]
- How to Send Email in Bash [2 Easy Methods]
- Bash Script to Send Email with Attachment [Step-by-Step Guide]
- How to Get IP Address in Bash [3 Methods]
- How to Find and Replace String in Bash [5 Methods]
- How to Get Script Name Using Bash Script? [3 Easy Ways]
- How to Call Another Script in Bash [2 Methods]
- How to Generate UUID in Bash [3 Simple Methods]
- 3 Easy Ways to Write to a File in Bash Script
- How to Write the Output to a File in Bash Script [5 Practical Cases]
- How to Create a List in Bash Scripts? [2 Easy Methods]
- How to Clear History in Bash [2 Practical Cases]
- How to Clear Screen Using Bash Script? [2 Effective Methods]
- How to Check Ubuntu Version Using Bash Scripts? [2 Methods]
<< Go Back to Bash Scripting Basics | Bash Scripting Tutorial