Home Learn Linux 10 must-know Bash shell scripting tips and tricks for beginners

10 must-know Bash shell scripting tips and tricks for beginners

We will provide some tips and tricks to help you become more proficient at Bash shell scripting. We will cover the use of comments, variables, quotes, conditionals, loops, functions, and command substitution.

by Arun Kumar
bash shell scripting tips and tricks for beginners

B

Bash shell scripting is a versatile and flexible tool for automating tasks and streamlining workflows in a Unix-like environment. It has become a popular language for both beginners and experienced developers alike. In this article, we will provide some tips and tricks to help you become more proficient at Bash shell scripting. We will cover the use of comments, variables, quotes, conditionals, loops, functions, and command substitution. By the end of this article, you will have a better understanding of how to use these features to write more efficient and effective Bash shell scripts.

Tips and Tricks for Bash Shell Scripting

1. Use Shebang Line

A shebang line is a special line at the beginning of a script that tells the shell which interpreter to use. It is important to include a shebang line in your Bash script so that it can be executed properly. The shebang line should start with “#!” followed by the path to the Bash interpreter. For example:

#!/bin/bash

2. Use Meaningful Variable Names

Using meaningful variable names can make your script more readable and easier to understand. Instead of using short, cryptic variable names, use descriptive names that indicate the purpose of the variable. For example, instead of using $a and $b, use $input_file and $output_file.

Practical Example: Let’s say we want to write a script that creates a backup of a directory and saves it to a specific location. Instead of using short, cryptic variable names, we’ll use descriptive names that indicate the purpose of each variable. For example:

#!/bin/bash

# Define source and destination directories
source_dir=/home/fosslinux/Documents/my_project
backup_dir=/home/fosslinux/Documents/backups

# Create a filename for the backup
backup_file=$backup_dir/$(date +%Y-%m-%d_%H:%M:%S)_my_project_backup.tar.gz

# Create the backup archive
tar -czf $backup_file $source_dir

# Print a message indicating the backup is complete
echo "Backup complete: $backup_file"
defining variables

Defining Variables

In this example, we’ve used meaningful variable names to make the script easier to read and understand. The source_dir variable represents the directory we want to back up, while the backup_dir variable represents the directory where we want to save the backup. The backup_file variable is used to create a filename for the backup that includes the current date and time.

By using descriptive variable names, it’s easier to understand what the script is doing and to make changes if necessary. For example, if we wanted to change the source directory, we could simply update the source_dir variable and the rest of the script would still work correctly.

3. Use Double Quotes for Variables

When you use variables in your Bash script, it is important to enclose them in double quotes to avoid word splitting and globbing. Word splitting occurs when the shell breaks up a string into separate words based on whitespace, which can cause problems if the string contains spaces or other special characters. Globbing occurs when the shell expands a string into a list of file names, which can also cause problems if the string contains wildcard characters such as * or ?.

# Correct way to use variables:
echo "The input file is: $input_file"

Practical Example: Let’s say we want to write a script that moves all text files from a source directory to a destination directory. We’ll use a variable to represent the source directory and another variable to represent the destination directory. Instead of using single quotes around the variables when we use them in the script, we’ll use double quotes to ensure that any spaces or special characters in the directory names are handled correctly.

#!/bin/bash

# Define source and destination directories
source_dir="/home/fosslinux/Documents/my project"
dest_dir="/home/fosslinux/Documents/my backups"

# Move all text files from source to destination
mv "$source_dir"/*.txt "$dest_dir"

In this example, we’ve used double quotes around the $source_dir and $dest_dir variables when we use them in the mv command. This ensures that any spaces or special characters in the directory names are handled correctly. If we had used single quotes instead, the command would not work correctly since there is a space between “my” and “project” as well as a space between “my” and “backups”.

script error as variable is not enclosed within a double quote

Script error as variable is not enclosed within a double quote

apply double quotes to variables example

Successful script execution after applying double quotes to variables

By using double quotes for variables, we can avoid unexpected behavior and ensure that our scripts work correctly in a variety of situations.

4. Use Braces for Variables

Using braces around variable names can help to avoid ambiguity and improve readability. This is particularly useful when using variables in complex expressions or when concatenating strings. For example:

# Example of using braces:
echo "The output file is: ${output_dir}/${output_file}"

Practical Example: Let’s say we want to write a script that creates a new directory with a name based on the current date and time. We’ll use the date command to get the current date and time, and then use braces to enclose the variable name when we use it in the mkdir command.

#!/bin/bash

# Create a new directory with a name based on the current date and time
new_dir="backup_$(date +%Y-%m-%d_%H:%M:%S)"
mkdir "$new_dir"

In this example, we’ve used braces around the date command when we use it to set the value of the $new_dir variable. This ensures that the output of the date command is included in the variable name. Without the braces, the $new_dir variable would contain the literal string “backup_date +%Y-%m-%d_%H:%M:%S”, rather than the output of the date command.

using braces for variables

Using Braces for variables

By using braces for variables, we can ensure that the variable name is expanded correctly and that our scripts work as intended.

5. Use Command Substitution

Command substitution is a technique that allows you to include the output of a command in a variable or in a command line. This can be useful for automating tasks or for generating dynamic output. To use command substitution, enclose the command in $() or backticks ( ).

# Example of using command substitution:
current_time=$(date +%Y-%m-%d_%H:%M:%S)
echo "The current time is: $current_time"

Practical Example: Let’s say we want to write a script that counts the number of files in a directory and displays a message indicating the total number of files. We’ll use the ls command with the -1 option to list all files in the directory, then use command substitution to count the number of lines in the output.

#!/bin/bash

# Count the number of files in a directory
file_count=$(ls -1 | wc -l)

# Display a message indicating the total number of files
echo "There are $file_count files in this directory."

In this example, we’ve used command substitution to count the number of lines in the output of the ls command. The $() syntax is used to enclose the ls command, which runs the command and returns the output as a string. The wc -l command is then used to count the number of lines in the output, which gives us the total number of files in the directory.

use command substitution

Using Command Substitution

By using command substitution, we can run commands within our scripts and use the output of those commands as part of our scripts. This allows us to automate tasks and perform complex operations in our scripts.

6. Use Conditional Statements

Conditional statements allow you to test a condition and execute different code depending on the result. This can be useful for handling errors or for implementing complex logic in your script. Bash supports several conditional statements, including if, elif, and else.

# Example of using if statement:
if [ -e $input_file ]; then
echo "The input file exists."
else
echo "The input file does not exist."
fi

Practical Example: Let’s say we want to write a script that checks if a file exists in a directory and displays a message indicating whether the file exists or not. We’ll use the test command with the -e option to check if the file exists, and then use an if statement to display the appropriate message based on the result of the test.

#!/bin/bash

# Check if a file exists in a directory
if test -e "/home/fosslinux/Documents/my backups/syslog_2.txt"; then
echo "The file exists."
else
echo "The file does not exist."
fi

In this example, we’ve used the test command with the -e option to check if the file /path/to/file.txt exists. The test command returns a zero status code if the file exists, and a non-zero status code if the file does not exist. We’ve then used an if statement to check the status code and display the appropriate message based on the result.

use conditional statements

Using Conditional Statements

By using conditional statements, we can make our scripts more flexible and responsive to different situations. We can perform different actions based on the results of tests, and ensure that our scripts behave correctly in a variety of scenarios.

7. Use Loops

Loops allow you to repeat a block of code multiple times, which can be useful for processing large amounts of data or for performing batch operations. Bash supports several types of loops, including for, while, and until.

# Example of using for loop:
for file in *.txt; do
echo "Processing file: $file"
done

Practical Example: Let’s say we want to write a script that lists all files in a directory, and then displays a message indicating the total number of files. We’ll use a for loop to iterate over all files in the directory, and a counter variable to keep track of the total number of files.

#!/bin/bash

# Initialize counter variable
file_count=0

# Loop through all files in directory and count them
for file in "/home/fosslinux/Documents/my backups/"*; do
if [ -f "$file" ]; then
file_count=$((file_count+1))
echo "$file"
fi
done

# Display a message indicating the total number of files
echo "There are $file_count files in this directory."

In this example, we’ve used a for loop to iterate over all files in the directory /path/to/directory. The loop runs once for each file in the directory, and the current file is stored in the $file variable. We’ve then used an if statement to check if the current file is a regular file, and increment the $file_count variable if it is. We’ve also used the echo command to display the current file.

using loops

Using Loops

By using loops, we can perform repetitive tasks in our scripts and automate complex operations. We can iterate over lists of files, directories, or other items, and perform different actions based on the items in the list. This allows us to create powerful and flexible scripts that can handle a variety of tasks.

8. Use Functions

Functions allow you to encapsulate a block of code and reuse it in different parts of your script. This can be useful for breaking down complex tasks into smaller, more manageable pieces. To define a function in Bash, use the syntax function_name() { code }.

# Example of using function:
function convert_file() {
input_file=$1
output_file=$2
# Code to convert file
}

Practical Example: Let’s say we want to write a script that performs a series of calculations on two numbers and displays the results. We’ll use a function to perform each calculation, and then call the functions with our input numbers to generate the output.

#!/bin/bash

# Define functions to perform calculations
function add {
echo $(($1 + $2))
}

function subtract {
echo $(($1 - $2))
}

function multiply {
echo $(($1 * $2))
}

function divide {
echo $(($1 / $2))
}

# Call the functions with input numbers
num1=10
num2=5

echo "Addition: $(add $num1 $num2)"
echo "Subtraction: $(subtract $num1 $num2)"
echo "Multiplication: $(multiply $num1 $num2)"
echo "Division: $(divide $num1 $num2)"

In this example, we’ve defined four functions to perform addition, subtraction, multiplication, and division on two input numbers. Each function takes two arguments ($1 and $2), which are the input numbers, and returns the result of the calculation. We’ve then called each function with our input numbers and used the echo command to display the result.

By using functions, we can modularize our scripts and break down complex tasks into smaller, more manageable components. We can create reusable functions that can be called from multiple parts of our script, and we can test each function independently to ensure that it works correctly. This allows us to create scripts that are more robust and easier to maintain.

9. Use Command-line Arguments

Command-line arguments allow you to pass values to your script when you run it from the command line. This can be useful for making your script more flexible and configurable. Bash provides several built-in variables that allow you to access command-line arguments, including $0 (the name of the script), $1, $2, $3, etc. (the first, second, third, etc. argument).

# Example of using command-line arguments:
input_file=$1
output_file=$2
# Code to process files

Practical Example: Let’s say we want to write a script that accepts two numbers as command-line arguments, and then performs a series of calculations on those numbers. We’ll use the $1 and $2 variables to access the input numbers, and then call functions to perform the calculations.

#!/bin/bash

# Define functions to perform calculations
function add {
echo $(($1 + $2))
}

function subtract {
echo $(($1 - $2))
}

function multiply {
echo $(($1 * $2))
}

function divide {
echo $(($1 / $2))
}

# Get input numbers from command-line arguments
num1=$1
num2=$2

# Call the functions with input numbers
echo "Addition: $(add $num1 $num2)"
echo "Subtraction: $(subtract $num1 $num2)"
echo "Multiplication: $(multiply $num1 $num2)"
echo "Division: $(divide $num1 $num2)"
bash fosslinux_bash_tips.sh 5 3

In this example, we’ve defined four functions to perform addition, subtraction, multiplication, and division on two input numbers. We’ve then used the $1 and $2 variables to get the input numbers from the command-line arguments. The $1 variable contains the first argument, and the $2 variable contains the second argument. We’ve then called each function with the input numbers and used the echo command to display the result.

use command line arguments

Use Command-line Arguments

By using command-line arguments, we can create more flexible and customizable scripts. We can pass input parameters to our scripts without having to modify the script itself, and we can create scripts that can be used in a variety of situations. This allows us to create powerful and versatile scripts that can handle a wide range of tasks.

10. Use Error Handling

Error handling is an important part of Bash scripting, as it allows you to handle unexpected errors or failures gracefully. Bash provides several mechanisms for error handling, including exit codes, error messages, and traps. It is important to include error handling in your scripts to ensure that they behave predictably and reliably.

# Example of using exit code and error message:
if [ ! -e $input_file ]; then
echo "Error: input file does not exist."
exit 1
fi

Practical Example: Let’s say we want to write a script that accepts two numbers as command-line arguments, and then performs a series of calculations on those numbers. We’ll use error handling to ensure that the input arguments are valid and that the script doesn’t crash if there’s an error.

#!/bin/bash

# Define functions to perform calculations
function add {
echo $(($1 + $2))
}

function subtract {
echo $(($1 - $2))
}

function multiply {
echo $(($1 * $2))
}

function divide {
if [ $2 -eq 0 ]; then
echo "Error: Division by zero"
exit 1
else
echo $(($1 / $2))
fi
}

# Check if input arguments are valid
if [[ ! $1 || ! $2 ]]; then
echo "Error: Missing input arguments"
exit 1
fi

# Get input numbers from command-line arguments
num1=$1
num2=$2

# Call the functions with input numbers
echo "Addition: $(add $num1 $num2)"
echo "Subtraction: $(subtract $num1 $num2)"
echo "Multiplication: $(multiply $num1 $num2)"
echo "Division: $(divide $num1 $num2)"

In this example, we’ve defined four functions to perform addition, subtraction, multiplication, and division on two input numbers. We’ve used an if statement to check if the input arguments are valid. If either argument is missing, we display an error message and exit the script with an error code. We’ve also added error handling to the divide function to ensure that we don’t attempt to divide by zero, which would cause an error.

error handling

Error Handling

By using error handling, we can create more robust and reliable scripts. We can prevent common errors and edge cases from causing our scripts to crash, and we can provide useful error messages to help users understand what went wrong. This allows us to create scripts that are more user-friendly and easier to troubleshoot.

Conclusion

Bash shell scripting is a powerful tool for automating tasks, managing systems, and performing a variety of other tasks. By using these ten must-know tips and tricks, you can write better and more efficient scripts that are easier to read, maintain, and debug.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.