UNIX Shell Scripting Tutorial for Beginners
Welcome to the dynamic space of UNIX Shell Scripting! This UNIX Shell Scripting tutorial will take you from a complete beginner to a confident shell scripter, and at the end of this you should be able to automate tasks, build powerful command line tools, and work more efficiently. If you’re ready to go deeper and get certified, go through our comprehensive UNIX Shell Scripting Course Syllabus to understand how to master these skills, with an expert leading the way.
Introduction to Shell Scripting
The shell is a command-line interpreter, a user interface to the kernel of an operating system. The shell is a program that enables you to communicate with the core of the operating system by entering commands.
Command-Line Interpeter
As a command-line interpreter, the shell reads in commands you type at a prompt, interprets them, and executes them.
For example, when you issue the command ls, which is a command that lists files, the shell converts it to a request that the kernel can understand, and executes it.
The shell provides the output back to you. The shell provides handles the input and output for your commands, provides scripting capabilities, and can control other programs.
User interface to the Kernel
To be clear, the kernel is the primary part of an operating system, and manages hardware, memory, and processes. The kernel does all of this under-the-hood, but you cannot talk to the kernel directly. This is where the shell comes in.
The shell serves as a “middleman” or layer of abstraction between you and the kernel. The user of the shell can create a directory, move a file, or even run an application without needing to know the low-level details of how the kernel manages that for you.
Introduction to Shell Script
A shell script is simply a text file that contains a sequence of commands that the shell can execute. Instead of typing commands one by one into the command line, you can put all the commands in one file and run it as a script.
Main Purpose: Automation and Task Flow Management
The main purpose of a shell script is to automate and manage task flows. This means you can use a script to automate repetitive tasks, thus saving time and making it less likely for you to make a mistake. For example, you may write a script to automatically back up files, install software, or process data.
Shell scripts can also be used to manage workflows, which is simply chaining multiple commands together to create a more complex task. In this example, the script checks for a file, compresses it, and uploads it to a server. Workflows allow you to manage and control a series of actions in a logical and repeatable way.
Why Learn Shell Scripting?
Learning shell scripting is useful because it provides an opportunity to automate repetitive tasks, work in an efficient manner, and improve your knowledge of how the operating system works.
Shell scripting is a fundamental skill for any work that involves a command-line environment, including development, administration, or anything else related to IT work.
Key Advantages of Shell Scripting
Automation: Shell scripts can automate mundane, cumbersome, and repetitive tasks. Instead of typing (or copy/pasting) a command sequence to perform an operation, you can write a script and just execute it.
- The often-repeated process can be significantly faster and less prone to human error! If you perform a task repeatedly (think compiling weighted .tar.gz compressed code or deploying scripts/applications), then this application will deliver real results for you!
Efficiency: Scripting is a way of thinking about a problem that allows you to write a powerful one-line command or small script to accomplish a task or perform an action quickly and efficiently.
- You can manipulate files easily, manage processes, and take care of data without the overhead of using a higher-level programming language. You can do this much faster and change your workflow into an efficient process!
- The history of command line to scripting includes some very high-level programming languages but there is even more functionality in lower-level utility! Just scripting will give you a powerful amount of tools.
Better Understanding of the OS: As you use loops, case patterns, and write scripts you will better understand how the OS manages files, directories, processes, and permissions.
- You will understand the command-line environment differently based on your understanding of how processes are initiated and how how different commands interrelate and interact with the kernel.
Real Life Examples
- File Backups: A simple script can copy important files on an automated schedule to a different location or a remote server. We can be sure our data is backed up, and not have to remember to do it manually.
- System Administration: System administrators use shell scripts to manage user accounts, check system health, monitor free disk space, and install software packages to multiple machines. This is important so that we can have a stable and secure computing environment.
- Data Processing: Researchers and data analysts use scripts to quickly process large text files. One of many examples includes a script that extracts certain pieces of data from specific logs, filters out information that is not needed, formats it as needed, etc.
Recommended: UNIX Shell Scripting Course Online.
Creating the First Shell Script
To start experimenting with shell scripting, let’s write a simple “Hello, World!” script. “Hello, World!” is typically the first program that most programmers write, and is a good exercise to illustrate the mechanics of creating and executing a script.
Step 1: Create the Script File
First, let’s open a text editor (e.g. nano, vim, gedit, etc.) and create a file named hello.sh. The .sh file extension is conventional for script files, but is not mandatory.
In your text editor, type the following two lines:
#!/bin/bash
echo “Hello, World!”
- #!/bin/bash is called a shebang. A shebang is the first line in a shell script. It instructs the operating system which interpreter to use to run the file. In this case, we are using the bash shell.
- echo “Hello, World!” is the command which displays the string “Hello, World!” in the terminal.
After typing the code, save and close the file.
Step 2: Make the Script Executable
New files are not executable by default. You need to give the file permission to run as a program. We do this with the chmod command to add executable permissions for the user who owns the file:
chmod u+x hello.sh
- chmod is short for “change mode”.
- u+x adds (+) the execute (x) permission for the user (u).
Step 3: Run the Script
There are two ways you can run your script:
- Using the path: ./hello.sh
- The ./ indicates that the script is in the current directory.
- Using the interpreter directly: bash hello.sh
- This explicitly states to the bash interpreter to run the script file. This memory method works even if the file does not have execute permission.
The output for both methods will be:
Hello, World!
Sample Script:
#!/bin/bash
# This is my first shell script
echo “Hello, World!”
Explore: UNIX Shell Scripting Interview Questions and Answers.
Variables and User Input
In UNIX shell scripting, you can use variables to hold and manipulate data, and you can use user input to make your scripts interactive.
Variables
A variable is just a name for a placeholder. In shell scripting, you don’t need to declare a variable type, you simply assign it a value.
Declaring Variables
To declare a variable, you should follow this syntax:
variable_name=value
Some important rules:
- There must be no spaces surrounding the equals sign (=).
- By convention, variable names are in uppercase.
Example:
NAME=”Alice”
AGE=30
Accessing Variables
To access a variable value, you must prepend its name with a dollar sign ($). This indicates to the shell that you are using the variable value as a literal string rather than the variable name.
Example:
#!/bin/bash
NAME=”Bob”
echo “Hello, $NAME!”
When you run this script, the output will be: Hello, Bob!
User Input
If you want your scripts to be interactive, you can ask a user to enter information. The read command is used to specify user input. It reads a single line of input typed at the terminal and saves it in a variable.
Using the read Command
The following is the basic syntax to use the read command:
read variable_name
Example:
#!/bin/bash
echo “Please enter your name:”
read USER_NAME
echo “Hello, $USER_NAME! Welcome to the script.”
When you run this script, it will first display the prompt: “Please enter your name:”. After typing your name and pressing enter, the script will save it into the variable USER_NAME and print a personalized greeting.
Using the -p Flag for read
The -p flag in the read command allows you to specify the prompt and combine it with the read command in a single line (this is a cleaner user experience).
Example:
#!/bin/bash
read -p “What is your favorite color? ” COLOR
echo “Your favorite color is $COLOR.”
This script will display the prompt immediately and wait for your input on the same line.
Conditional Statements in UNIX Shell Scripting
In UNIX shell scripting, conditional statements are used to make decisions in your scripts based on whether something is true or false. The primary conditional statement will be the if statement.
The if Statement
If a condition is true, the if statement only runs a block of code. An if statement’s basic grammar is as follows:
if [ condition ]
then
# commands to execute if the condition is true
fi
- The condition is enclosed in square brackets “[]”. You must put spaces around the brackets and the condition itself.
- The then statement below specifies the line where the commands to be run, if the condition is true, starts.
- The fi statement marks the end of the if statement (if spelled backwards).
if-else statement
The if statement can be extended for use with an else block. An else block provides an alternative set of commands to run if the condition is false.
if [ condition ]
then
# commands for true
else
# commands for false
fi
if-elif-else statement
In situations where you have more complicated logic to evaluate in your script, you can use an elif (which stands for else if) statement that will evaluate condition after condition in a linear sequence.
if [ condition1 ]
then
# commands for condition1
elif [ condition2 ]
then
# commands for condition2
else
# commands for all other cases
fi
Common Conditions/Operators
Shell scripts use a number of operators to compare strings, numbers and the properties of files.
String Comparisons
- = or ==: Check if two strings are equal.
- !=: Check if two strings are not equal.
- -z: Check if the string is empty (zero length).
- -n: Check if the string is not empty.
Example:
if [ “$USER” == “root” ]
then
echo “You are the root user.”
fi
Numerical Comparisons
- -eq: Is equal to
- -ne: Is not equal to
- -gt: Is greater than
- -ge: Is greater than or equal to
- -lt: Is less than
- -le: Is less than or equal to
Example:
COUNT=10
if [ $COUNT -gt 5 ]
then
echo “The count is greater than 5.”
fi
File Conditions
- -e: Tests if a file or directory exists.
- -f: Tests if the specified path is a regular file.
- -d: Tests if the specified path is a directory.
- -r, -w, -x: Tests if a file is readable, writable, or executable.
Example:
if [ -f “myfile.txt” ]
then
echo “myfile.txt is a regular file.”
else
echo “myfile.txt does not exist or is not a regular file.”
fi
Sample UNIX Script: Simple Number Comparison
#!/bin/bash
echo “Enter a number:”
read NUM
if [ $NUM -gt 10 ]; then
echo “The number is greater than 10.”
elif [ $NUM -eq 10 ]; then
echo “The number is exactly 10.”
else
echo “The number is less than 10.”
fi
Loops in UNIX Shell Scripting
When writing UNIX shell scripts, loops allow you to repeatedly execute a block of code. Loops allow you to automate tasks that you need to perform based on a list of items or until a specific condition has been satisfied. The most common types of loops are for and while loops.
for Loop
The for loop is primarily used to iterate over a list of items, such as files, numbers, or strings. The for loop will run the commands declared within the loop only once for each item in the list. The syntax is as follows:
for variable in list
do
# commands to execute for each item
done
- variable is a temporary variable assigned the value of the current item in the list during the loop iteration.
- list is the list of items we want to iterate over.
Example: Iterating Over A List of Names
#!/bin/bash
for NAME in “Alice” “Bob” “Charlie”
do
echo “Hello, $NAME”
done
This is the output of the script above:
Hello, Alice
Hello, Bob
Hello, Charlie
Example: Processing files
You can use a for loop with a wildcard (*) to iterate through all the files in a directory.
#!/bin/bash
for file in *.txt
do
echo “Processing file: $file”
done
The previous script will use the wildcard (.) to find all the files in the current directory ending with .txt and print their names.
while Loop
The while loop executes a block of code while a predetermined condition is true. The while loop is useful in situations when you do not know how many times you are going to loop.
while [ condition ]
do
# commands to execute as long as the condition is true
done
- condition is one test the loop checks before each iteration. The loop continues, as long as the condition evaluates to true.
Example: Counting from 1 to 5
#!/bin/bash
COUNT=1
while [ $COUNT -le 5 ]
do
echo “Count: $COUNT”
COUNT=$((COUNT + 1)) # Increment the variable
done
This scripts starts with COUNT at 1. The loop continues as long as COUNT is less than or equal to 5. The loop code prints the value of COUNT, increments COUNT by 1 (using ((…)), which just performs arithmetic).
The output will be:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Sample Script: File Backup
#!/bin/bash
echo “Starting file backup…”
for FILE in *.txt; do
cp “$FILE” “${FILE}.bak”
echo “Backed up $FILE to ${FILE}.bak”
done
echo “Backup complete!”
Explore: Hardware and Networking Course in Chennai.
Functions in UNIX Shell Scripting
Functions in UNIX shell scripting allow you to group together a set of commands and name them. This allows your scripts to be more organized, readable, and reusable. Instead of having to copy and paste the same block of code many times and then work to separate them again, you can simply call the function by name.
Definition of a Function
There are two primary syntaxes or methods to define a function in a shell script.
Using the function keyword:
function my_function {
# commands to be executed
}
Without the function keyword:
my_function () {
# commands to be executed
}
Both of these syntaxes fulfill the same purpose. The commands contained inside of the curly braces {} are the body of the function.
Calling a Function
To execute the codes contained inside a function you simply call the name of the function.
Example:
#!/bin/bash
# Function definition
greet_user () {
echo “Hello, there!”
echo “This is a simple function.”
}
# Call the function
greet_user
Output
Hello, there! This is a simple function.
Passing Arguments to a Function
Functions may take arguments, which are values that are passed to the function when it is called. The arguments that are passed to the function are available to the function in a set of special variables:
- $1, $2, $3, etc.,: These three refer to the first, second, and third arguments passed to the function.
- $#: It shows how many arguments were supplied to the function.
- $* or $@: A list of individual strings or a full string of arguments can be used to access all of the parameters supplied to the function.
Example:
#!/bin/bash
greet_name () {
echo “Hello, $1!”
echo “You passed $# arguments to this function.”
}
# Call the function with an argument
greet_name “Alice”
greet_name “Bob” “Smith”
Output
Hello, Alice!
You passed 1 arguments to this function.
Hello, Bob!
You passed 2 arguments to this function.
Returning a Value from a Function
You can return a value from a function with the return command. The return value can be an integer between 0 (inclusive) and 255 (inclusive). A return value of 0 usually means success and a non-zero value indicates an error.
The return value can be obtained with $? immediately after the function call.
Example:
#!/bin/bash
check_file () {
if [ -f “$1” ]; then
return 0 # Success: file exists
else
return 1 # Failure: file does not exist
fi
}
# Call the function and check the return value
check_file “myfile.txt”
if [ $? -eq 0 ]; then
echo “File exists.”
else
echo “File not found.”
fi
Advanced Topics in UNIX Shell Scripting
Here are the brief notes on advanced concepts in UNIX:
Pipes and Redirection:
- A pipe (|) is a powerful feature that connects a command’s standard output to another command’s standard input. This feature allows you to chain together commands and create a powerful data processing pipeline.
- Redirection allows you to alter the standard input and standard output of a command. By default, the input of a command will come from the keyboard (standard input or stdin) and the output will display on the screen (standard output or stdout).
Command-Line Arguments:
- Command-line arguments are used to supply a shell script with information by means of arguments at the time you call it. The use of arguments allows you to make your scripts flexible and dynamic, and based on the input the script can take different actions.
Error Handling:
- Error Handling in UNIX shell scripting is necessary if you want to offer reliability in your scripts for handling unexpected situations.
- Error handling allows the script to recognize when a command has failed and take some actions, instead of just continuing and possibly causing other failures.
Case Statement:
- A case statement in UNIX shell scripting is a better alternative to a long chain of if-elif-else if statements.
- The case statement allows you to match a variable or expression against multiple patterns in the case statement header and execute a block of code based on the first pattern that matched.
- This is a pretty cool feature because it cleans up the code and makes it easier to read, especially since you could have many possible conditions.
Explore: All Software Training Courses.
Conclusion
You should now have the basic building blocks of UNIX shell scripting. You have enough understanding to write your first script and set up functions and get user input. You should be ready to start automating tasks and working more productively.
This UNIX Shell Scripting tutorial is just the tip of the iceberg. Shell scripting is a vast and powerful world. If you want to truly understand and develop your shell scripting skills and have a portfolio of really handy scripts, you should consider enrolling in a structured course.
Our UNIX Shell Scripting Course provides structured lessons, hands-on labs, and helps from our experts in becoming a certified professional in Unix Shell Scripting. Are you ready to improve your skills? Register for our class today!