Series: Linux/Unix Tutorial

Shell Scripting Tutorial

Start your journey into shell programming with our beginner's guide. Learn the fundamentals of shell scripting, basic commands, and practical examples to master the essentials of shell programming.
E
Edtoks3:15 min read

Shell scripting is a powerful way to automate tasks and perform various operations in Unix-like operating systems. In this explanation, I'll cover some basic shell programming concepts with examples using the Bash shell, which is one of the most commonly used Unix shells.

1. Shebang (#!/bin/bash):

The shebang line, #!/bin/bash, is placed at the beginning of a shell script to indicate that it should be executed using the Bash shell.

Example:

#!/bin/bash

 

2. Comments:

Comments are used to provide explanations within a script. They begin with the # symbol and are ignored by the shell when the script is executed.

Example:

# This is a comment

3. Variables:

Variables are used to store data and values in shell scripts. They can be created without specifying a data type.

Example:

name="John"
age=30

4. Echo (Printing Output):

The echo command is used to display text or variable values on the screen.

Example:

echo "Hello, World!"
echo "Name: $name, Age: $age"

5. User Input:

You can prompt the user for input using the read command and store the input in a variable.

Example:

echo "Enter your name:"
read user_name
echo "Hello, $user_name!"

6. Conditional Statements (if-else):

Conditional statements are used to make decisions in scripts. if and else are keywords used for branching.

Example:

age=18
if [ "$age" -ge 18 ]; then
    echo "You are an adult."
else
    echo "You are a minor."
fi

7. Loops (for and while):

Loops are used to repeat a block of code multiple times. Common loop types are for and while.

Example (for loop):

for i in {1..5}; do
    echo "Iteration $i"
done

Example (while loop):

count=1
while [ $count -le 5 ]; do
    echo "Count: $count"
    count=$((count+1))
done

8. Functions:

Functions allow you to define reusable blocks of code within a script.

Example:

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

greet "Alice"
greet "Bob"

9. Command Line Arguments:

You can pass arguments to a shell script when you run it. These arguments are stored in special variables like $1, $2, etc.

Example:

# script.sh
echo "Argument 1: $1"
echo "Argument 2: $2"

Run the script with arguments:

./script.sh arg1 arg2

10. Exit Status:

A script can exit with a status code using the `exit` command. Zero (0) indicates success, while non-zero values indicate failure

Example:

  if [ "$age" -ge 18 ]; then
      echo "You are an adult."
      exit 0
  else
      echo "You are a minor."
      exit 1
  fi

This is just a basic introduction to shell scripting in Unix. Shell scripting can become quite complex and powerful, allowing you to automate tasks, process data, and interact with the system in various ways. As you gain more experience, you can explore advanced topics such as file handling, string manipulation, and error handling to create more sophisticated scripts.