Skip to content

00. Linux Shells and Shell Scripting

What is a Shell?

A shell is a command-line interface (CLI) that allows users to interact with the operating system. It acts as a bridge between the user and the kernel, interpreting commands and executing them.

  • User enters command → Shell interprets → Kernel executes → Output returned

Check Current Shell

echo $SHELL

Example Output

/bin/bash

Explanation

  • $SHELL is an environment variable.

  • It stores the path of the current default shell.


List All Available Shells

cat /etc/shells

Example Output

/bin/bash
/usr/bin/zsh
/usr/bin/fish

Explanation

  • This file contains all shells installed on the system.

  • Useful when changing your default shell.


Switch Shell (Temporary)

zsh

Explanation

  • Starts a new shell session (Zsh here).

  • Does not change default shell.

To return:

exit

Change Default Shell (Permanent)

chsh -s /usr/bin/zsh

Explanation

  • chsh = change shell

  • -s specifies the shell path

  • Takes effect after logout/login


Feature Bash Zsh Fish
Default in Linux Yes No No
Scripting Strong Strong Limited
Auto-completion Basic Advanced Very advanced
Customization Moderate Very high High
Plugins Limited Extensive (Oh My Zsh) Built-in features

What is a Shell Script?

A shell script is a file containing a sequence of commands executed by the shell.

  • File extension: .sh

  • Used for automation, system tasks, and scripting workflows


Creating and Running a Script

Step 1: Create File

nano script.sh

Step 2: Add Code

#!/bin/bash
echo "Hello World"

Explanation

  • #!/bin/bash → Shebang (defines interpreter)

  • echo → Prints output


Step 3: Make Executable

chmod +x script.sh

Explanation

  • Adds execute permission

Step 4: Run Script

./script.sh

Variables in Shell

#!/bin/bash

echo "Enter your name:"
read name

echo "Hello, $name"

Explanation

  • read name → takes user input

  • $name → accesses variable value


Loops

For Loop

#!/bin/bash

for i in {1..5}
do
  echo "Number: $i"
done

Explanation

  • {1..5} → sequence generator

  • Loop runs 5 times

  • $i → current iteration value


While Loop

#!/bin/bash

count=1

while [ $count -le 5 ]
do
  echo $count
  ((count++))
done

Explanation

  • Runs while condition is true

  • -le means "less than or equal"

  • ((count++)) increments value


Conditional Statements

#!/bin/bash

echo "Enter username:"
read user

if [ "$user" = "admin" ]; then
  echo "Access granted"
else
  echo "Access denied"
fi

Explanation

  • if [ condition ] → evaluates condition

  • = → string comparison

  • then → executes if true

  • else → executes if false

  • fi → ends block


Command Line Arguments

#!/bin/bash

echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"

Run:

./script.sh hello world

Output:

Script name: ./script.sh
First argument: hello
Second argument: world

Explanation

  • $0 → script name

  • $1, $2 → arguments passed


Functions in Shell

#!/bin/bash

greet() {
  echo "Hello, $1"
}

greet Subrat

Explanation

  • greet() defines function

  • $1 inside function → argument


Comments

# This is a single-line comment

Best Practice

  • Use comments to explain logic

  • Improve readability and maintainability


File Operations Example

#!/bin/bash

file="test.txt"

if [ -f "$file" ]; then
  echo "File exists"
else
  echo "File not found"
fi

Explanation

  • -f → checks if file exists

  • Useful in automation scripts


Exit Status

#!/bin/bash

ls file.txt
echo $?

Explanation

  • $? → last command status

  • 0 → success

  • non-zero → error


Best Practices

  • Always use shebang (#!/bin/bash)

  • Use meaningful variable names

  • Quote variables: "$var"

  • Add comments for complex logic

  • Test scripts before deployment


Summary

Concept Description
Shell Interface between user and OS
Script File with executable commands
Variables Store values
Loops Repeat execution
Conditionals Decision making
Functions Reusable blocks
Permissions Required to execute scripts