Introduction to Linux Shell Scripting

Overview:

Whether we talk about any Process Automation or delivering the projects before deadline, the core fundamentals heavily relies on the Linux Shell Scripting. In this blog, we’ll cover the basic aspects of bash scripting along with the examples. Lets start with few core definitions:

What is Shell?

A Shell is a command line user interface for Unix-like operating systems. It can be referred as a computer program that provides access to an operating system’s services for human users or other programs in the field of computing.

What is Bash Shell?

Bash referred as Bourne Again Shell, replacing the older version Bourne shell in 1989. Its command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell.

It is a Default shell on most of the Linux Operating Systems and MacOS.

What is Bash Scripting?

A bash script is a file that consists of a series of commands executed by the bash program in sequential order. It enables you to perform various actions, such as changing to a particular directory, creating directories, and executing command-line processes.

Some Useful Commands

mohitchaudhary@MOHITs-MacBook-Air Library % cd $HOME
mohitchaudhary@MOHITs-MacBook-Air ~ % pwd
/Users/mohitchaudhary
mohitchaudhary@MOHITs-MacBook-Air ~ % ls -l
total 16
drwxr-xr-x  14 mohitchaudhary  staff   448 Feb  1  2022 AndroidStudioProjects
drwx------@  5 mohitchaudhary  staff   160 Mar 17  2021 Applications
-rw-r--r--   1 mohitchaudhary  staff     0 May 28  2022 DO NOT UPLOAD FILES HERE
drwx------@  8 mohitchaudhary  staff   256 May 15 13:55 Desktop
drwx------@ 12 mohitchaudhary  staff   384 May 12 12:46 Documents
drwx------@  9 mohitchaudhary  staff   288 May 17 22:38 Downloads
drwx------+ 93 mohitchaudhary  staff  2976 Nov  7  2022 Library
drwx------   8 mohitchaudhary  staff   256 Apr  7 09:35 Movies
drwx------+  5 mohitchaudhary  staff   160 Apr  7 09:35 Music
drwx------+  8 mohitchaudhary  staff   256 Mar  5  2022 Pictures
drwxr-xr-x+  4 mohitchaudhary  staff   128 Mar  2  2021 Public
drwxr-xr-x   4 mohitchaudhary  staff   128 Jul  9  2021 PycharmProjects
drwxr-xr-x@ 21 mohitchaudhary  staff   672 Oct 18  2021 StudioProjects
drwxr-xr-x@  6 mohitchaudhary  staff   192 Apr 30 20:33 dwhelper
-rw-r-----   1 mohitchaudhary  staff   233 Nov  2  2022 mohitchaudhary-WebLogicConfig.properties
-rw-r-----   1 mohitchaudhary  staff    80 Nov  2  2022 mohitchaudhary-WebLogicKey.properties
mohitchaudhary@MOHITs-MacBook-Air ~ % chmod 755 mohitchaudhary-WebLogicKey.properties
mohitchaudhary@MOHITs-MacBook-Air ~ % cp mohitchaudhary-WebLogicKey.properties backup_key.properties
mohitchaudhary@MOHITs-MacBook-Air ~ % rm -rf backup_key.properties

Working with Variables

Variables are the entities in which data can be stored. By using the variables, you can read, edit, access and manipulate the data within the scripts. Bash does not have distinct data types. Instead, variables in Bash can store various forms of data, including numeric values, single characters, or strings of characters.

Lets understand with few examples:

Assigning the values directly:

bash-3.2$ username=mohit

Lets print out the value:

bash-3.2$ echo username
username
bash-3.2$ echo $username
mohit

As you can see above, if we do echo only username then it will print that particular Variable Name. In order to print the value that Variable is storing, we have to use $, before variable name. In this case, $username

Environment Variables

Environment variables are mutable values that impact the behavior of processes or programs running on a computer. They are present in all operating systems, although their types may differ. Environment variables can be generated, modified, stored, and removed, providing insights into the system’s behavior. Below are the some working examples of Environment Variables:

bash-3.2$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.9/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
bash-3.2$ meme
bash: meme: command not found
bash-3.2$ export PATH=$PATH:/usr/local/meme/bin
bash-3.2$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.9/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/meme/bin

Let’s understand with sample script file (goodmorning.sh):

bash-3.2$ vi goodmorning.sh

##Sample Code##
#!/bin/bash
echo good morning $username , have a great day! 

bash-3.2$ chmod +x goodmorning.sh 
bash-3.2$ ./goodmorning.sh 
good morning , have a great day!

The above script won’t work as the variable username is not exported. In the next step, we will export the variable and will again run the script.

bash-3.2$ export username=mohitchaudhary
bash-3.2$ ./goodmorning.sh 
good morning mohitchaudhary , have a great day!

As you can see clearly, this time script has pickup our username as we have exported the same. The more conventional way to use the variable names in Capital letters i.e. export USERNAME=mohitchaudhary

Tip: Use export keyword to see all the exported environment variables.

bash-3.2$ export
declare -x HOME="/Users/mohitchaudhary"
declare -x HOMEBREW_CELLAR="/opt/homebrew/Cellar"
declare -x HOMEBREW_PREFIX="/opt/homebrew"
declare -x HOMEBREW_REPOSITORY="/opt/homebrew"
declare -x INFOPATH="/opt/homebrew/share/info:"
declare -x JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home"
declare -x LC_CTYPE="UTF-8"
declare -x LOGNAME="mohitchaudhary"
declare -x MANPATH="/opt/homebrew/share/man::"
declare -x OLDPWD
declare -x PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/meme/bin"
declare -x PWD="/Users/mohitchaudhary"
declare -x SHELL="/bin/zsh"
declare -x SHLVL="3"
declare -x SSH_AUTH_SOCK="/private/tmp/com.apple.launchd.koelt8IDmh/Listeners"
declare -x TERM="xterm-256color"
declare -x TERM_PROGRAM="Apple_Terminal"
declare -x TERM_PROGRAM_VERSION="444"
declare -x TERM_SESSION_ID="9BDAB14F-95A5-4A53-B49C-5CA1BA30D0D9"
declare -x TMPDIR="/var/folders/yc/pbs_lfjs3d173wdzwk7db9vc0000gn/T/"
declare -x USER="mohitchaudhary"
declare -x XPC_FLAGS="0x0"
declare -x XPC_SERVICE_NAME="0"
declare -x __CFBundleIdentifier="com.apple.Terminal"
declare -x username="mohitchaudhary"

Working with Arrays

An array is a structured organization of data elements of the same kind. However, in a Shell script, an array refers to a variable that can hold multiple values, which may be of the same type or different types. In Shell script, all values are treated as strings by default.

#!/bin/bash

#Declaring an Static Array
fruitsArray=('Banana' 'Apple' 'Mango' 'Orange')

#To Print Single Item
echo ${fruitsArray[0]}
echo ${fruitsArray[1]}
echo ${fruitsArray[2]}

#To Print all items in Array
echo ${fruitsArray[*]}

Below is the output:

bash-3.2$ vi arrays.sh
bash-3.2$ chmod +x arrays.sh 
bash-3.2$ ./arrays.sh 
Banana
Apple
Mango
Banana Apple Mango Orange
bash-3.2$

Using Quotation Marks and String Handling in Bash

It’s very important to understand the key difference between single and double quotes when we are dealing in scripts and usage of either can make our script stuck.

Remember, In bash Expressions are evaluated inside “..”, NOT in ‘..’

bash-3.2$ USERNAME="Mohit"
bash-3.2$ echo 'Hello $USERNAME'
Hello $USERNAME
bash-3.2$ echo "Hello $USERNAME"
Hello Mohit
bash-3.2$

In simpler terms, use ${…} to access strings and substrings. Below are the examples of the same:

bash-3.2$ echo username
username
bash-3.2$ echo $username
mohit

Arithmetic Expansion in Bash

Below are the working examples of command substitution that are used for the arithmetic expansion in Bash Scripting:

bash-3.2$ USERNAME="Mohit"
bash-3.2$ echo 'Hello $USERNAME'
Hello $USERNAME
bash-3.2$ echo "Hello $USERNAME"
Hello Mohit
bash-3.2$

By default Bash only handles Integer types. You can use bc to perform the floating calculations as shown in below example:

bash-3.2$ echo 'scale=4;5/3' | bc
1.6666

Hope it helps. Cheers!!!

About the author

Mohit Chaudhary

Hey there, I am IT enthusiast who is passionate about Middleware, DevOps, Cloud and much more.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *