Bash Script Examples

Bash script is a plain text files with the “.sh” extension. It 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.

Bash scripting also known as shell scripting is 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 that we call commands. Moreover, it allows user to write their own script and interact with the system resources as per needs. This article provides a few Bash script examples that aim to teach you to write Bash script from scratch.

Where to Write a Bash Script?

To write a Bash script, you can use any of the text editors, as Bash scripts are essentially text files with a .sh extension. 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, ensure that the script has the necessary execution permissions. Once the permissions are set, you can execute the script by calling it from the terminal. The command syntax for executing a bash file is chmod +x <bash file name>.

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.

1. Starting With Hello World

Let’s start with a Bash script that can print “hello world”. The purpose of this script is to show you how echo command works in Bash. Go through the below steps:

  1. At first, launch an Ubuntu Terminal.
  2. Open a file with Nano editor. For example nano hello.shCreating-a-.sh-file
  3. Copy the following script and paste it into the file. Then press CTRL+O and ENTER to save the file; CTRL+X to exit.
    #!/bin/bash
    echo  “Hello World”
    EXPLANATION
    Here, 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”.
  4. Running the file will display a message as below:Executing-hello.sh-file

Note: The quotation marks around “Hello World” should be straight quotation marks () instead of curly quotation marks (“”). Deliberately used curly quotation marks are treated as part of the string itself.

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.

To define variables use the following bash script:

#!/bin/bash
name=Tom
age=12
echo "$name's age is $age"
EXPLANATION

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.

Variables-in-Bash-scriptThe script prints “Tom’s age is 12” by incorporating the values of the variables’ name and age using variable expansion by a dollar sign ($).

Some key points to note from the example:

  • 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.

3. Environment Variables in Bash

Environment variables in Bash are dynamic named values that define the operating environment for the shell session and its child processes. 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. Some of the most common environment variables are:

  • 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.

The below script shows the value of the two most important environment variables HOME and PATH:

#!/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, $HOME and $PATH is used to extract the home directory and value of the PATH variable respectively.Environment-variables-in-Bash Once you execute the above script, the values of the variables HOME and PATH will be displayed, as shown in the output image.

4. Delete or Unset Variable

Users can delete or unset a variable using the unset command in Bash scripting. Unsetting a variable makes the variable undefined or empty. Here’s an example script that shows the usage of unset command:

#!/bin/bash

name="LinuxSimply"
echo "Name before deletion: $name"
unset name
echo "Name after deletion: $name"
EXPLANATION

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.

Bash-script-example-of-unsetting-variablesAfter unsetting the variable name, the echo command can’t retrieve its value as earlier.

5. Getting the Script Name

One can easily find the name of a Bash script using the basename command. Let’s create a script to return back its name:

#!/bin/bash

script_name=$(basename "$0")
echo "The script name is: $script_name"
EXPLANATION

In the above script, basename is used to extract the 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.

Finding-script-name-in-BashThe script prints its name (name.sh).

Note: You can use $BASH_SOURCE instead of $0 to get the name of the script.

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 the user along with taking input. Later, you can use echo $VARIABLE_NAME to display the user input on the screen.

To take user input in a variable, use the below bash script:

#!/bin/bash

read -p "Enter a number:" num
echo "You entered: $num"
EXPLANATION

The given 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.

Input-in-BashHere 25 is the user input that is saved in the num variable. Later the program recalls the user input using the echo command.

7. Display Output in Terminal or Saving in a File

Let’s see 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 about its usage:

#!/bin/bash

read -p "Enter a number: " num1
read -p "Enter another number: " num2
add=$((num1 + num2))
echo "Addition of numbers: $add"
EXPLANATION

The script allows users to input two numbers and calculate 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.

Displaying-output-in-BashThe 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 of 15 in the terminal.

Saving Output in a File

Here is how you can redirect the output to a file:

#!/bin/bash

read -p "Enter a number: " num1
read -p "Enter another number: " num2
add=$((num1 + num2))
echo "Addition of numbers: $add" > output.txt
EXPLANATION

The provided script allows users to input two numbers and calculate 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.

Writing-output-to-a-fileHere 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.

8. Calculation of Floating Point Using Pipe

Arithmetic calculation on floating point numbers can be done using pipe (|). It’s a mechanism to connect the output of one command as an input of another command. Here’s a practical example:

#!/bin/bash

result=$(echo "3.14 + 2.7" | bc)
echo "$result"
EXPLANATION

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.

Calculating-floating-number-in-BashOnce executed the program prints the summation of two floating point numbers 3.14 and 2.7.

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. if can check single or multiple conditions using only one if statement. The following script shows a simple example of if conditionals:

#!/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
EXPLANATION

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.”

If-statement-in-BashThe above image shows that when the user input is -5, the program executes the code within the else block as the condition is not true.

Note: Ensure accurate whitespace usage within the condition of the if statement to avoid errors. Pay attention to both missing and extra whitespaces.

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

10. “Or/And” Condition in If Statement

Sometimes users may want to check multiple conditions in a single if statement. Logical operators are quite helpful for this purpose. Some of the Logical operators of the Bash script are:

  • &&: AND operator
  • ||: OR operator
  • !: NOT operator

To use or/and condition within bash if condition, go through the following script:

#!/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
EXPLANATION

The 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.” Else, 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-condition-in-BashIn the above output, with an age of 22 and marks of 780, the only condition under the second elif holds true and the script outputs “Sorry, you are not eligible for the scholarship.”

11. Checking Empty Variable

Use -z to check whether a variable is empty or not. The below script shows how to use it:

#!/bin/bash

var=""
if [ -z "$var" ]; then
echo "The variable is empty."
else
echo "The variable is not empty."
fi
EXPLANATION

[ -z "$var"] returns True if $var has not any value set. Writing this inside an if block checks the return value and executes the corresponding code block. If the variable is empty, the script executes code under the if block and prints the message “The variable is empty.” Otherwise, it executes the code within the else block.

Checking-empty-variables-in-BashAs the variable is empty, the program executes the if block to echo the proper message, as highlighted in the image above.

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. Here’s an example of case statement in Bash script:

#!/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
EXPLANATION

The 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.

Case-statement-in-BashThe 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.

13. “for” Loop in Bash

Bash for loop is a fundamental construct in shell scripting for this purpose. Every programming language offers some control structure for looping over an iterable. The script below shows the use of a for loop for counting files of the directory:

#!/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"
EXPLANATION

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.

For-loop-in-BashThe image shows output1.txt and output.txt are two existing txt files in the current directory.

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:

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..."
EXPLANATION

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 the input is not equal to “q” or not equal to “Q”. If the user’s input is not q or Q, it prompts a message by 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.

While-loop-in-BashHere, you can see the user’s 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.

15. “until” Loop in Bash

Until is a loop in bash and 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. To use the until loop within a practical use, see the following script:

#!/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."
EXPLANATION

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.

Until-loop-in-Bash-scriptYou 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.

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. Here’s a script showing the usage of select:

#!/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
EXPLANATION

This Bash script demonstrates the use of a select loop to create a menu-driven interface. The array options hold 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.

Select-structure-in-BashHere user first inputs r, which is an invalid option. Later Option 1 is chosen. Finally, the user quits the program by pressing 4.

17. Loop With Break or Continue

Bash offers continue and break statement to skip or terminate the program. This gives the flexibility to control the flow of the loops in an efficient manner. The below script demonstrates the use of these:

#!/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
EXPLANATION

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 will be displayed, and the user will be 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.

Controling-loop-in-BashHere the first input is a wrong password. However, the user can’t see the input as it is hidden using -s option. The 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 reaches the break statement.

18. String Operation in Bash

The script below showcases the basic operations on strings such as concatenation, length calculation, and substring extraction in Bash:

#!/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
EXPLANATION

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 the 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.

String-manipulation-in-BashFor 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.

19. Iterating Over Bash Array

There are mainly two types of arrays in Bash. They are indexed arrays and associative arrays. An indexed array can be easily accessed using the index of each item. In the following script, I will show you how to iterate over an indexed array. The below script shows the use of a loop to iterate over an array:

#!/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
EXPLANATION

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.

Array-in-BashOnce executed the program prints all the items of myArray in a new line.

Note: Items of an indexed array are separated by whitespaces. However, space within the quotation is treated as part of the string.

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.

To declare an associative array, use the following bash script:

#!/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"]}"
EXPLANATION

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.

Associative-array-in-BashWhen executed the program accesses the value of the “red” key by using ${colors["red"]} and display it with the echo command.

21. Getting Bash Array by Changing IFS Value

The IFS (Internal Field Separator) is a special variable that determines how Bash recognizes word boundaries or field separators within strings or input read by the shell. It’s used by the shell to split strings into words or fields when using constructs like loops or command substitution. By default, IFS is set to whitespace characters: space, tab, and newline. However, it can be customized to any character or sequence of characters. The below script shows how to change IFS:

#!/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[*]}"
EXPLANATION

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[*]}.

Converting-into-Bash-arrayHere fruit names are separated by a comma. The program read names from the input.txt file and created a space-separated array as displayed.

22. Creating Bash Function

Function is a feature of Bash and other programming languages that helps in writing cleaner and more modular code by allowing you to break down complex tasks into smaller, manageable parts. The following example demonstrates the creation of a simple Bash script function:

#!/bin/bash
# Function definition
greet() {
read -p "Enter your name:" name
echo "Hello, $name! Welcome to Linuxsimply!!"
}

# Function call with an argument
greet
EXPLANATION

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“.

Bash-script-FunctionsHere user put the name Alina. The program gives a welcome message to Alina as shown in the image.

23. Functions With Argument

Arguments are values or variables passed to the function when it’s called. These arguments provide data to the function, allowing it to perform actions or computations based on the provided values.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. Here’s how:

#!/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
EXPLANATION

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.

Function-with-argumentHere user wants to get the factorial of 5. 120 is shown as the factorial of the given number in the next line.

24. Get Date in Bash

The below Bash script prints the date in YY-MM-DD format:

#!/bin/bash

today=$(date +%y-%m-%d)
echo $today
EXPLANATION

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.

Getting-date-in-BashThe program prints the current date 23-07-11 as specified by the format specifiers.

25. Send Mail in Bash

Bash supports sending email using the mailx command. The below script shows how to use mailx to send email through Bash:

#!/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"
EXPLANATION

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.

When executed, this script will send an email to the recipient ([email protected]) with the subject “Leave” and the given body text.

26. Calling Another Script

To call another script,  use the bash command or add the path of the calling script in the PATH variable. The script below shows both processes:

Using bash command:

#!/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

Using PATH variable:

#!/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"
EXPLANATION

The first script 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 the second script, 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.

Calling-another-script-in-BashHere, 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.

Note: Make sure both of the scripts are executable.

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. Here’s the script to get the ip address of a computer:

#!/bin/bash

ip_address=$(ifconfig | grep 'inet ' | awk '{print $2}')
echo $ip_address
EXPLANATION

The provided Bash script retrieves and displays the IP address of a specified network interface. By default, it finds both the 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.

Getting-IP-address-in-BashThe 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.

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.

To handle errors using bash exit status, run the following bash script:

#!/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."
EXPLANATION

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.

Error-in-Bash-scriptThe 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

What is the difference between shell scripting and Bash scripting?

A shell is a program that is used for interacting with the system. Examples of popular shells include Bash, sh, csh, and ksh. On the other hand, Bash scripting refers to interacting with the machine by writing codes and commands using Bash shell only.

Is Bash script is a text file?

Yes, a Bash script is a plain text file that contains a series of commands and typically has a .sh extension, distinguishing it from regular text files that often have a .txt extension.

Why people use Bash scripting?

People use Bash scripting for automating tasks, system administration, data processing, log analysis, and many more.

Can I execute Bash script in Python?

Yes, you can execute a Bash script in Python by using the subprocess module to run the Bash script as a subprocess. This allows you to invoke and execute Bash commands or scripts within your Python code.

What are the differences between the while and until loop?

The while loop executes until a zero status is returned whereas the until loop executes until a nonzero status is returned. While loop may execute single or multiple times as long as the condition is met. On the other hand, the until loop always executes at least once.

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment