Shell Scripting Basics
Shell scripting is a way to automate tasks in the Unix shell. It involves writing commands in a script file, which can then be executed by the shell to perform a series of tasks.
Here are a few basic concepts that you should understand when learning about shell scripting:
Shell: A shell is a command-line interpreter that allows you to interact with the operating system.
Use shebang lines: A shebang line is a special line at the beginning of a script that specifies the interpreter to use. For example, a shebang line of #!/bin/bash
tells the system to use the Bash interpreter to run the script.
Script: A script is a text file that contains a series of commands that are executed by the shell. Scripts are usually written in a language that the shell can understand, such as Bash, but they can also include commands written in other languages, such as Python or Ruby.
Variables: Variables are named storage locations that hold data. You can use variables in a shell script to store and manipulate data, such as strings, numbers, and arrays.
Conditional statements: Conditional statements allow you to control the flow of your script based on certain conditions. For example, you can use an if statement to execute a certain block of code only if a certain condition is met.
Loops: Loops allow you to repeat a block of code multiple times. There are several types of loops available in shell scripting, including for loops, while loops, and until loops.
Exit status: Every command in a shell script returns an exit status code when it finishes execution. The exit status code is a number that indicates whether the command was successful or encountered an error. You can use the exit status code to control the flow of your script and handle errors.
Comments: Comments are lines in a script that are ignored by the shell. You can use comments to document your code and make it easier to understand. In Bash, comments are denoted by a #
symbol.
Shell commands and interpretation
Bash (Bourne Again SHell) is a Unix-like shell that is widely used on Linux systems. It is a command-line interpreter that reads and executes commands from the user or from a script file.
When you enter a command in the Bash shell, the interpreter reads the command and checks to see if it is a built-in command. If it is a built-in command, the interpreter executes the command directly. If it is not a built-in command, the interpreter searches for the command in the directories listed in the PATH
environment variable. If the command is found, the interpreter executes the command. If the command is not found, the interpreter displays an error message.
For example, if you enter the command ls
, the interpreter will check to see if ls
is a built-in command. If it is not, the interpreter will search for an executable file named ls
in the directories listed in the PATH
variable. If the ls
executable is found, the interpreter will execute it. If the ls
executable is not found, the interpreter will display an error message.
In addition to searching the directories listed in the PATH
variable, the Bash interpreter also checks to see if the command is a shell script or an alias. If the command is a shell script, the interpreter will execute the script. If the command is an alias, the interpreter will execute the command or commands that the alias is set to.
Bash scripts are text files that contain a series of commands to be executed by the Bash interpreter. To run a Bash script, you can use the bash
command followed by the name of the script file, like this:
bash script.sh
Bash scripts are often used to automate tasks or perform multiple commands in a single operation. They can include variables, loops, conditional statements, and other programming constructs, and they can be used to perform a wide variety of tasks.
The PATH
is an environment variable in the Linux operating system that specifies a list of directories that the shell searches when you enter a command.
The PATH
variable is a colon-separated list of directories that the shell checks in order, from left to right, when you enter a command. For example, if the PATH
variable is set to /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin
, the shell will search for the command in the following directories:
/usr/local/bin
/usr/bin
/usr/local/sbin
/usr/sbin
If the command is found in one of these directories, the shell will execute it. If the command is not found, the shell will display an error message.
You can use the echo
command to print the value of the PATH
variable:
echo $PATH
You can also use the export
command to set or modify the value of the PATH
variable:
export PATH=$PATH:/new/directory
This will add the /new/directory
directory to the end of the PATH
variable.
Writing our first shell script
To write an executable shell file, you can create a text file that contains a series of shell commands, and then make the file executable by setting the appropriate permissions. Here is an example of how you can create an executable shell file:
- Open a text editor and type the commands that you want to include in the script. For example:
#!/bin/bash
echo
"Hello, world!"
The first line is called a shebang line and specifies the interpreter to use to run the script. In this case, the script will be run using the Bash interpreter.
- Save the file with a
.sh
extension, for examplemyscript.sh
. - Open a terminal and navigate to the directory where you saved the script.
- Make the script executable by using the
chmod
command:
chmod +x myscript.sh
- Run the script by typing its name:
./myscript.sh
This will execute the script and print “Hello, world!” on the screen.
Shebang line in shell scripts
A shebang line is a special line at the beginning of a script file that specifies the interpreter to use to run the script. It is denoted by the #!
characters, followed by the path to the interpreter.
For example, the following shebang line specifies that the script should be run using the Bash interpreter:
The shebang line must be the first line of the script file, and it must be followed by a newline character.
The shebang line is used to tell the system which interpreter to use to run the script. When you run a script, the system checks the shebang line to determine which interpreter to use. If the shebang line is not present, or if it specifies an interpreter that is not available on the system, the script will not be executed.
Shebang lines are commonly used in shell scripts, but they can also be used in scripts written in other languages, such as Python or Ruby.
Variables in shell scripting
In the Unix shell, variables are named storage locations that hold data. You can use variables in the shell to store and manipulate data, such as strings, numbers, and arrays.
Here are a few examples of how you can use variables in the shell:
To declare a variable, you can use the VARNAME=value
syntax, where VARNAME
is the name of the variable and value
is the value you want to assign to the variable. For example:
$ x=10
x
and assigns it the value 10
.To reference a variable, you can use the $VARNAME
syntax, where VARNAME
is the name of the variable. For example:
$ echo $x
x
variable, which is 10
.To manipulate a variable, you can use arithmetic operators or string manipulation commands. For example:
$ x=$((x + 1))
x
variable by 1
.$ y="hello"
$ z="$y world"
This declares a variable named y
with the value hello
, and a variable named z
with the value hello world
.
Shell scripts and positional variables
In a shell script, positional variables are variables that are set to the values of the command line arguments passed to the script when it is run. The positional variables are numbered, starting with $0
for the name of the script itself, and $1
, $2
, etc. for the individual arguments.
Here is an example of a simple shell script that prints the values of the positional variables:
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
To run this script, you can use the bash
command followed by the name of the script file and the arguments you want to pass to the script, like this:
$ bash script.sh arg1 arg2
This will execute the script and print the following output:
Script name: script.sh
First argument: arg1
Second argument: arg2
You can use the positional variables in your script to access and manipulate the arguments passed to the script. For example, you can use them in arithmetic expressions or string manipulations.
To access all of the arguments passed to a shell script in Linux, you can use the $@
special variable. The $@
variable is an array that contains all of the arguments passed to the script, including the script name itself ($0
).
Here is an example of a simple shell script that prints all of the arguments passed to the script:
echo "Number of arguments: $#"
echo "All arguments: $@"
To run this script, you can use the bash
command followed by the name of the script file and the arguments you want to pass to the script, like this:
$ bash script.sh arg1 arg2 arg3
This will execute the script and print the following output:
Number of arguments: 3
All arguments: arg1 arg2 arg3
${}
notation. The ${}
notation allows you to specify the variable name as a string, rather than as a variable.For example, to access the 10th positional variable, you can use the ${10}
notation:
echo "The 10th argument is: ${10}"
You can also use the ${}
notation to access variables that have been assigned names.
set
command to add positional parameters
In Bash, the set
command is used to set or unset shell options and positional parameters, as well as to display the values of all shell variables.
Here are some examples of how set
can be used:
# Set the value of the first positional parameter (also known as $1) to "foo"
set foo
# Set the value of the second positional parameter (also known as $2) to "bar"
set foo bar
# Set the value of the third positional parameter (also known as $3) to "baz"
set foo bar baz
# Set the value of multiple positional parameters at once
set foo bar baz qux quux
# Unset a positional parameter
set --
shift command to remove positional parameters
In shell scripts, the shift
command is used to shift the values of positional parameters (also known as arguments) to the left. When shift
is run, the value of $2
becomes $1
, the value of $3
becomes $2
, and so on. The value of $1
is lost.
Here’s an example of how shift
can be used in a Bash script:
# Set the values of the positional parameters
set foo bar baz qux quux
# Print the values of the positional parameters
echo "Positional parameters before shift: $@"
# Shift the positional parameters to the left
shift
# Print the values of the positional parameters again
echo "Positional parameters after shift: $@"
This script sets the values of the positional parameters to foo
, bar
, baz
, qux
, and quux
, and then prints them. It then runs the shift
command, which shifts the values of the positional parameters to the left. Finally, it prints the values of the positional parameters again, which should now be bar
, baz
, qux
, and quux
.
You can specify a number as an argument to shift
to shift the positional parameters multiple times at once. For example, shift 2
would shift the positional parameters twice, so that the value of $3
becomes $1
, the value of $4
becomes $2
, and so on.
Environmental variables in Shell Programming
In the Unix shell, environmental variables are named variables that are set by the system or by the user and are available to all processes on the system. Environmental variables can be used to store information that is needed by programs or scripts, such as the system’s hostname, the user’s home directory, or the system’s path.
To view the list of environmental variables that are set on your system, you can use the env
command. This will print a list of all of the environmental variables and their values.
Here is a list of some important environmental variables in Linux:
HOME
: The home directory of the current user.PATH
: A list of directories where the system looks for executables.SHELL
: The path to the current user’s default shell.USER
: The name of the current user.HOSTNAME
: The hostname of the system.PWD
: The current working directory.LANG
: The default system language.LC_ALL
: The default language and locale settings for the system.EDITOR
: The default text editor.MAIL
: The path to the user’s mailbox.
These are just a few examples of the many environmental variables that are available in Linux. Environmental variables can be used to store information that is needed by programs or scripts, such as the system’s hostname, the user’s home directory, or the system’s path.
To use an environmental variable in a shell script, you can use the $
notation followed by the name of the variable. For example, to access the value of the HOME
environmental variable, which specifies the home directory of the current user, you can use the following syntax:
$ echo "The value of HOME is: $HOME"
You can also use environmental variables in commands or scripts. For example, to list the files in the home directory of the current user, you can use the ls
command and the $HOME
environmental variable like this:
$ ls $HOME
Summary:
Shell scripting is a way to automate tasks in the Unix shell by writing commands in a script file that can be executed by the shell. In shell scripting, you can use shebang lines to specify the interpreter to use, variables to store and manipulate data, conditional statements to control the flow of the script, loops to repeat a block of code, input and output to read from and write to files and programs, exit status codes to handle errors, comments to document code, and functions to reuse and organize code. Bash is a popular Unix-like shell that is used on Linux systems, and it reads and executes commands from the user or from a script file. You can run a Bash script by using the bash command followed by the name of the script file, or you can make the script file executable and run it directly. In Bash, you can also use special characters and operators to perform tasks such as redirection, substitution, and expansion. Shell scripting is a useful tool for automating tasks and creating programs in the Unix shell.
Questions for review:
- How does the Bash interpreter determine how to execute a command that is entered by the user?
- How can you run a Bash script?
- What is the PATH environment variable and what is its purpose?
- How can you create an executable shell script?
- What is a shebang line and what is its purpose?
- Can you provide an example of a shebang line that specifies the Bash interpreter?
- How can you print the value of the PATH variable using the Bash shell?
- How can you modify the value of the PATH variable using the Bash shell?