Bash scripts come in handy for automating tasks, and you’ll find they’re great for building simple command line applications. The Bash shell interprets Bash scripts, so you won’t need to install any dependencies to write and run them. Bash scripts are also portable since most Unix-based operating systems use the same shell interpreter.

Variables in Bash

Bash variables are case-sensitive. To declare variables, use an equals sign (=) with the name on the left and value on the right:


The value this declaration assigns to STATE is a single word. If you need spaces in your value, use quotes around it:

STATE="North Arizona"

You’ll need to use a dollar sign ($) prefix to reference variables in other variables or statements:

LOCATION="My Location is $STATE"

Printing Values in Bash

There are several ways you can print variables in Bash. You can use the echo command for basic output or the C-style printf command for string formatting.

LOCATION="My Location is $STATE"

After declaring the STATE variable, this script defines LOCATION by referencing STATE. If then uses echo to print the final value of the LOCATION variable.

The printf keyword allows you to use formatting verbs to output data. The string formatting verbs are similar to the ones in C and Go but with limited verbs.

Verb Functionality
%c prints single characters
%o prints Octadecimals
%s prints strings, independent of casing
%x prints lowercase hexadecimal
%X prints uppercase hexadecimal
%d prints integers
%e prints scientific notion floats in lowercase
%E prints scientific notion floats in uppercase
%f prints floating point numbers
%% prints a single percentage symbol.

Here’s an example of using a verb with the print keyword.

printf "My Location is %s" $STATE

The printf function would substitute the STATE variable in the position of the %s verb, and the output would be “My Location is Lagos”.

Comments in Bash

You can make comments in Bash with the hash or pound (#) symbol. The shell automatically ignores comments.

# STATE=Washington
# LOCATION="My Location is $STATE"

There are no multi-line comments. Most IDEs and text editors allow you to comment with the Ctrl/Command + forward slash(/) shortcut. You should be able to use the shortcut to create multiple single-line comments.

Receiving User Input in Bash

Like many other programming languages, you can receive user input in Bash to make your programs/scripts more interactive. You can use the read command to request the user’s input.

read response

In this case, the response variable will hold the user’s input on delivery.

echo "What do you want ?: "
read response
echo $response

The user input request will be on a new line in the example above.

You can add the -n flag to the echo print statement to retain the line where the user enters input.

echo -n "What do you want."
read response
echo $response

Declaring Arrays in Bash

Arrays in Bash are just like most languages. You can declare an array variable in Bash by specifying the elements in parentheses.

Countries=('USA' 'Russia' 'Ukraine', "England", "Taiwan", "China")

Accessing an array via reference to the variable name would fetch the first element. You can access the entire array by using the asterisk sign as the index.

echo ${Countries[*]}

You can also specify the index of the array to access a specific element. The index of an array starts at zero.

echo "${Countries[4]}"

Conditional Statements in Bash

Bash provides conditionals for decision-making in programs.

Here’s the anatomy of an if-else statement in Bash. You’ll have to use the semi-colon to specify the end of the condition.

if [[ condition ]]; then
   echo statement1
elif [[condition ]]; then
   echo statement2
else [[condition ]]; then
   echo statement3

You must end every if statement with the fi keyword.

if [ 1 == 2 ]; then
   echo one 
elif [ 2 == 3 ]; then #else-if
   echo two
else [ 4 > 3 ]; 
   echo "correct, 3"

You can use case statements in your Bash programs using the case keyword. You’ll have to specify the pattern followed by ending parentheses before the statement.

case $CITY in
  "Washington") # pattern 
    echo "United States of America" # statement
    ;; # end of a case
  "Lagos" | "Abuja")
    echo  "Nigeria"
  "Johannesburg" | "Cape Town")
    echo  "South Africa"
  *) # default pattern
    echo "Antarctica" # default statement
esac # end of the case statement

You can define the default case using the asterisk (*) sign as the pattern. Case statements must end with the esac keyword.

Loops in Bash

Depending on your needs, you can use a while loop, range for-loop, or a C-style for loop for recurring operations.

Here’s an example of the C style for-loop. For-loops must end with the done keyword, and you must end the for statement with a semicolon followed by the do keyword.

for ((a = 0 ; a < 10 ; a+2)); do
  echo $a

The range for loop comes in handy for working with files and many other operations. You’ll need to use the in keyword with the range for-loop.

for i in {1..7}; do
    echo $1

Here’s a simple infinite loop to demonstrate Bash while loops in action.

while [ 1 -le 5 ] # while 1 < 5
  echo $name

The -le in the condition statement is the binary operator for less than.

Functions in Bash

You don’t need keywords to declare functions in Bash. You can declare functions with the name and then parentheses before the function’s body.

print_working_directory() {
  echo $PWD  #calling the PWD command from the script
echo "You are in $(print_working_directory)"

Functions can return variables in Bash. All you need is the return keyword.

print_working_directory() {
  return $PWD

The print_working_directory function returns the working directory of the file.

You Can Write Shell Scripts in Other Languages

Bash isn’t the only language you can use to interact with your operating system’s shell or build command-line applications. You can use many other languages like Go, Python, Ruby, and Rust.

Many operating systems have Python3 pre-installed, and Python is a prevalent language. If you need even more functionality than Bash scripts can offer, consider using Python.