Bash Shell Scripting Guide

Bash Shell Scripting Guide

·

4 min read

Featured on daily.dev

Introduction to Shell

Whenever you log in to a Linux system you are placed in a shell program. The shell's prompt is usually visible at the cursor's position on your screen. To get your work done, you enter commands at this prompt.

The shell is a command interpreter, it takes each command and passes it to the operating system kernel to be acted upon. It then displays the results of this operation on your screen.

Different users may use different shells. Initially, your system administrator will supply a default shell, which can be overridden or changed. The most commonly available shells are:

• Bourne shell (sh)

• C shell (csh)

• Korn shell (ksh)

• TC Shell (tcsh)

• Bourne Again Shell (bash)

You can see the list of available shells in your Linux

Screenshot from 2022-01-20 16-31-41.png

What is a Shell Script?

A shell script is an executable file containing multiple shell commands that are executed sequentially. The file can contain:

  • Shell (#!/bin/bash)
  • Comments (# comments)
  • Commands (echo, cp, grep, etc.)
  • Statements (if, while, for, etc.)

• Shell script should have executable permissions (e.g. -rwx r-x r-x)

• Shell script has to be called from absolute path (e.g /home/userdir/script.bash)

• If called from current location then ./script.bash

Basic Scripts

  • Output to the screen using “echo”

  • Creating tasks

    • Telling your id, current location, your files/directories, system info

    • Creating files or directories

    • Output to a file “>”

  • Filters/Text processors through scripts (cut, awk, grep, sort, uniq, wc)

• Defining Variables :

Screenshot from 2022-01-20 16-50-37.png

• Read Input :

Screenshot from 2022-01-20 16-55-06.png

Screenshot from 2022-01-20 16-58-09.png

• Scripts to run commands within

Screenshot from 2022-01-20 17-00-55.png

Special Variables and Positional Arguments

Special Variables are used in the names of special Unix variables. These variables are reserved for specific functions.

Positional parameters are the arguments given to your scripts when it is invoked. It could be from $1 to $N. When N consists of more than a single digit, it must be enclosed in braces like ${N}.

$0 is the name of the script itself (script.sh).
$1 is the first positional argument (filename1)
$2 is the second positional argument (dir1)
$3 is the last argument of the script (10.0.0.1)
$9 would be the ninth argument and ${10} the tenth


$# is the number of the positional arguments
"$*" is a string representation of all positional arguments: $1, $2, $3 ....
$? is the most recent foreground command exit status

if-then Scripts

The if...else...fi statement is the next form of control statement that allows Shell to execute statements in a controlled way and make the right choice.

Syntax:

if [ some_condition_is_true ]
then
//execute this code
elif [ some_other_condition_is_true ]
then
//execute_this_code
else
//execute_this_code

Comparisons:

 -eq  equal to for numbers
 ==   equal to for letters
 -ne  not equal to
 !==  not equal to for letters
 -lt    less than
 -le   less than or equal to
 -gt  greater than 
 -ge greater than or equal to

File Operations:

-s file exists and is not empty
-f file exists and is not a directory
-d directory exists
-x file is executable
-w file is writable
-r file is readable

Let's take an example to check if a variable value is met.

Screenshot from 2022-01-20 17-17-46.png

Screenshot from 2022-01-20 17-18-30.png

For Loop Scripts

Keep running until the specified number of variable e.g: variable = 10 then run the script 10 times OR variable = green, blue, red (then run the script 3 times for each color).

Syntax:

for item in LIST
do
COMMANDS
done

Let's take an example to create 5 files named 1-5. Screenshot from 2022-01-20 17-26-46.png

do-while Scripts

The while statement continually executes a block of statements while a particular condition is true or met.

Syntax:

while [ condition ]
do
   command1
   command2
   commandN
done

Case Statement Scripts

The case statement is a good alternative to the multilevel if-then-else-fi statement. It enables you to match several values against one variable. It is easier to read and write.

Syntax:

case EXPRESSION in
PATTERN_1)
STATEMENTS
;;
PATTERN_2)
STATEMENTS
;;
PATTERN_N)
STATEMENTS
;;
*)
STATEMENTS
;;
esac

Example:

Screenshot from 2022-01-20 18-34-17.png Output: Screenshot from 2022-01-20 18-34-33.png

Aliases

The alias command allows you to define new commands. Useful for creating shortcuts for longer commands. The syntax is.

alias alias-name=executed_command

Some examples:

alias m=more
alias rm="rm -i"
alias h="history -r | more"

To view all current aliases:

alias

To remove a previously defined alias:

unalias alias_name

Did you find this article valuable?

Support Rudrakshi by becoming a sponsor. Any amount is appreciated!