Bash Scripting for Beginners (Part 1 — Introduction and Variables)

Ahmet Okan YILMAZ
4 min readApr 7, 2022

Shell scripting is an important part of process automation in Linux. Shell scripts are used by Systems Administrators, Programmers, Network Engineers, Scientists, Data Engineers, DevOps Engineers and just about anyone else who uses a Linux system regularly. You can generally find a way to use Bash scripting to make your life easier.

Shell is an environment in which we can run our commands, programs, and shell scripts. There are two major types of shells: Bourne Shell and C Shell. The Bourne Shell has subcategories: Bourne shell (sh), Korn shell (ksh), Bourne Again shell (bash), POSIX shell (sh).

The default shell for many Linux distros is the Bourne-Again Shell (bash).

Bash scripts can be used for various purposes, such as executing a shell command, running multiple commands together, customizing administrative tasks, performing task automation etc.

Command Prompt

When a shell is used interactively, it displays a $ when it is waiting for a command from the user. This is called the shell prompt: [root@host ~]$

If shell is running as root, the prompt is changed to #. The superuser shell prompt looks like this:[root@host ~]#

Bash Script

A Bash script is a plain text file which contains a series of commands. These are read and executed by the bash program. The program executes line by line.

Bash scripts usually end with a .sh. However, bash scripts can run perfectly fine without the sh extension.

Scripts are also identified with a shebang. Shebang is a combination of bash # and bang ! followed the the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell.

#! /bin/bash

Running Your First Script

Create a file named hello.sh

touch hello.sh

Open the file with a text editor like nano.

nano hello.sh

Write the commands.

#! usr/bin/bash
echo "Hello World"

Type Ctrl+X, press Y and Enter.

Modify the file permissions and allow execution of the script.

$ chmod u+x hello.sh

Then, run the script.

$ ./hello.sh

or

$ bash ./hello.sh

The output will look like this:

Hello World

Variables

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, etc.

Variable Names

The name of a variable can contain only letters, numbers or the underscore character (_).

Variable Types

There are three main types of variables: Local, environment and shell.

Local Variables: A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.

Environment Variables: An environment variable is available to any child process of the shell. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.

Shell Variables: A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly.

Define a Variable

Variables are defined as follows:

NAME="Ahmet Okan YILMAZ"AGE=36

Accessing Values

To access the value stored in a variable, we use dollar sign ($). The following script will define a variable and then prints it.

#! usr/bin/bashNAME="Ahmet Okan YILMAZ"
echo $NAME

The output will look like this:

Ahmet Okan YILMAZ

Read-only Variables

If a variable is marked read-only, its value cannot be changed.

#!/bin/bash

NAME="Ahmet Okan YILMAZ"
readonly NAME
NAME="Selim Batur YILMAZ"

When the script run, the output wiil be following:

/bin/bash: NAME: This variable is read only.

Unset a Variable

Once you unset a variable, you cannot access the stored value in the variable. You cannot use the unset command to unset a variable that are marked readonly.

#!/bin/bash

NAME="Ahmet Okan YILMAZ"
unset NAME
echo NAME

This script doesn’t print anything.

Special Variables and Arguments

There are some special variable names that we can not use a variable name.

  • $0: The name of the Bash script.
  • $n: n is a positive deciamal number. $1 stands for first argument.
  • $#: How many arguments were passed to the Bash script.
  • $@: All the arguments supplied to the Bash script.
  • $?: The exit status of the most recently run process.
  • $$: The process ID of the current script.
  • $!: The process number of the last background command.
  • $USER: The username of the user running the script.
  • $HOSTNAME: The hostname of the machine the script is running on.
  • $SECONDS: The number of seconds since the script was started.
  • $RANDOM: Returns a different random number each time is it referred to.
  • $LINENO : Returns the current line number in the Bash script.

Following script uses some special variables:

#!/bin/bashecho "File Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "Third Argument: $3"
echo "Total Number of Parameters : $#"

The output:

$./test.sh Ahmet Okan YILMAZ
File Name : ./test.sh
First Parameter : Ahmet
Second Parameter : Okan
Third Parameter : YILMAZ
Total Number of Parameters : 3

Quotes

Single Quotes: Enclosing characters in single quotes (’) preserves the literal value of each character within the quotes.

Double Quotes: Enclosing characters in double quotes (“) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’.

echo "$(echo "hello there")"
> hello there
echo '$(echo "hello there")'
> (echo "hello there")

I will continue in the next article.

--

--

Ahmet Okan YILMAZ

Industrial Engineer | Data Scientist | Factory Manager