Bash Scripting : Variables and Shell Expansions - Part 2

In this tutorial, we will be looking into more of BASH's syntax. This can help you in writing excellent scripts using the full capability of the BASH language. We will be focusing mainly on Variables and Shell Expansions in this tutorial. Variables and shell expansions are absoluteley core features for even basic BASH scripts. We will learning all about user-defined variables, environment variables, various parameter expansions, command substitution, arithmetic expansion, tilde expansion and brace expansion to make use of all the features provided by BASH.

User defined variables and Parameters

What is a Parameter?

A Parameter is an entity that can store values. Also, parameters are of different types, and they are as follows

  1. Variables (User defined variables and Shell variables)
  2. Positional Parameters
  3. Special Parameters

Variable is a parameter whose values you can manually change. You might have come across variable in several other programming languages and here also it serves the same purpose. The main purpose of a variable is to store data. You can assign any data to a variable and you can retrieve them at any time during the course of the execution of your program.

How to create a user defined variable in a bash script?

#!/bin/bash
name="Sebin Sebastian"

In the above script, name is our variable and we are storing the value 'Sebin Sebastian' in the name variable. It is critically important that there should be no spaces around the = sign and it can result in fatal errors. If you do leave a space around the name variable, BASH will think that name is a command and it will throw an error.

Now what we created above is called an User Defined variable in BASH. Later, we will come across Shell Variables which are inturn created for us by BASH.

Useful Info

In bash scripting, it is customary to declare all user defined variable names in lower case whereas the shell variables are always in uppercase letters. This helps to easily distinguish between an user defined variable and a shell variable.

How to retrieve data from a variable?

#!/bin/bash
name="Sebin Sebastian"

echo "Hello, ${name}"

image.png As you can see, we successfully printed back the name that we stored in the name variable.

The ${ } is the syntax that we used to reference the parameter or the user defined variable. Whenever the shell sees this ${ },it will replace it with it's value. This process is called Parameter Expansion.

Useful Info

What if the shell can't find the parameter with the name you have given?

The shell will then replace the ${ } with pure emptiness and it will be like there is nothing.

Shell Variables (Environment Variables)

Until now, we discussed user defined variables which were created by the user. Now we will look at shell variables which are created by the bash itself. In this section, we will look at some commonly used shell variables and their uses. We will be looking at the difference between bourne shell variables and bash shell variables. Also, we will be looking some of the commonly used shell variables such as the PATH, HOME, USER, HOSTNAME, PS1 etc.

What are shell variables?

Shell variables can be classified into two types. They are as follows

  1. Bourne Shell Variables

    This was the initial shell that was developed by Stephen Bourne in 1979. There are approximately 10 bourne shell variables.

  2. Bash Shell Variables

    As time went on, the bash came in to existence and it is based onthe initial bourne shell. In bash, there exists eround 95 shell variables which are not in the available in bourne shell.

Common Shell Variables

  • PATH Variable

The PATH variable contains the list of directories that the shell will search for executable files to run as command names.

$ echo ${PATH}
  • HOME Variable

The HOME variable is used to store the absolute path to the current user's home directory.

$ echo ${HOME}
  • USER Variable

The USER variable contains the username of the current user.

$ echo ${USER}
  • HOSTNAME Variable

The HOSTNAME variable contains the name of the current computer.

$ echo ${HOSTNAME}
  • HOSTTYPE Variable

The HOSTTYPE variable tells you what type of processor architecture the computer is running.

$ echo ${HOSTTYPE}
  • PS1 Variable The PS1 variable contains the prompt string shown in the terminal before each command.

Useful parameter expansion tricks

Following commands will show you more advanced ways in which you could expand your parameters.

name=sEBin
  1. Full Uppercase

In order to make the first letter uppercase, use the following syntax

echo ${name^}

In order to make all letters uppercase, use the following syntax

echo ${name^^}
  1. Full Lowercase

In order to make the first letter lowercase, use the following syntax

echo ${name,}

In order to make all letters lowercase, use the following syntax

echo ${name,,}