An introduction to shell scripting fundamentals covering script creation execution, and the shebang directive. Learn how to write and execute simple bash scripts with practical examples.
This document introduces the fundamentals of shell scripting in Linux environments. It explains what scripts are, how they are executed, and the importance of the shebang directive. Through a step-by-step example of creating a simple "Hello World" script, readers will learn the basic workflow of writing, making executable, and running shell scripts, providing a foundation for automating tasks and developing more complex scripts.
A script is a text file containing a sequence of commands that can be interpreted and executed by a specific program or scripting language. Scripts serve as a powerful way to automate tasks in Linux environments.
At its core, a script is a list of commands that:
Unlike compiled programs, scripts are not converted into machine code before execution. Instead, they are processed and executed line by line at runtime by an interpreter. This makes them:
Shell scripts are versatile tools that find applications in numerous areas:
| Use Case | Description |
|---|---|
| System Administration | Automating routine maintenance tasks, user management, and system monitoring |
| ETL Jobs | Extracting, transforming, and loading data between systems |
| File Operations | Automating backups, archiving, and file system maintenance |
| Application Integration | Connecting different software components and systems |
| Deployment Automation | Streamlining software deployment processes |
| Web Development | Creating server-side applications and automation tools |
| Custom Utilities | Building specialized tools for specific workflows |
The flexibility of shell scripts makes them indispensable for both beginners and experienced system administrators.
The first line of a shell script typically contains a special directive known as the “shebang” (a combination of “sharp” and “bang” for the #! characters).
The shebang directive tells the system which interpreter should be used to execute the script. Its syntax is:
1#!interpreter [optional-argument]
Where:
#! - The literal characters that mark the beginning of the shebanginterpreter - An absolute path to the program that should interpret the script[optional-argument] - An optional string representing a single argumentDifferent scripting languages use different interpreters, which are specified in the shebang:
| Shebang | Description |
|---|---|
#!/bin/sh | Uses the Bourne shell or a compatible shell |
#!/bin/bash | Specifically uses the Bash shell |
#!/bin/zsh | Uses the Z shell |
#!/usr/bin/perl | Interprets the script as a Perl program |
#!/usr/bin/env python3 | Uses the environment to locate Python 3 |
The shebang line is crucial because it determines how the script will be interpreted and executed.
Creating a simple “Hello World” script is an excellent way to understand the basics of shell scripting. The following steps demonstrate how to create, make executable, and run a basic shell script.
First, create an empty text file with the .sh extension, which is a convention used to indicate shell scripts:
1touch hello_world.sh
Add the shebang line to specify that the script should be interpreted by the Bash shell:
1echo '#!/bin/bash' > hello_world.sh
Append the command to print “Hello World” to the script:
1echo 'echo Hello World' >> hello_world.sh
Before running the script, it must be made executable using the chmod command:
1chmod +x hello_world.sh
File permissions in Linux are divided into three categories:
| Permission | Symbol | Meaning |
|---|---|---|
| Read | r | The file can be viewed |
| Write | w | The file can be modified |
| Execute | x | The file can be run as a program |
To check the current permissions of a file, use:
1ls -l hello_world.sh
The output might look like:
1-rw-r--r-- 1 user group 29 Jul 3 14:45 hello_world.sh
After making the file executable with chmod +x, the permissions would change to:
1-rwxr-xr-x 1 user group 29 Jul 3 14:45 hello_world.sh
The x in the permissions indicates that the file is now executable.
Now that the script is executable, it can be run:
1./hello_world.sh
This will output:
1Hello World
The ./ prefix is necessary to tell the shell to look for the script in the current directory.
When a shell script is executed, the following sequence of events occurs:
This process allows scripts to automate complex sequences of commands that would otherwise need to be entered manually.
Shell scripts offer several advantages but also come with certain limitations.
Understanding these trade-offs helps in deciding when shell scripting is the appropriate solution for a given task.
Shell scripting is a fundamental skill for Linux system administration and automation. By understanding the basics of what scripts are, how the shebang directive works, and the process of creating and executing scripts, a solid foundation is established for more advanced scripting techniques. The simple “Hello World” example demonstrates the essential workflow of creating, making executable, and running scripts that can be expanded to automate complex tasks and streamline workflows.
Interpreted scripts differ from compiled programs in several key ways:
chmod +x filename.sh command makes a shell script executable in Linux. The chmod command changes the file mode bits, and the +x option adds execute permission for all user categories (owner, group, and others).Common use cases for shell scripting include:
File permissions for shell scripts follow the standard Linux permission model with three categories (owner, group, others) and three permission types (read, write, execute):
For a script to be executable, it must have the execute permission set, which is typically done with the command chmod +x scriptname.sh.
The sh (Bourne shell) and bash (Bourne Again SHell) differ in several ways:
(2) A shebang directive is a line at the beginning of a script that starts with #! followed by the path to an interpreter. It tells the operating system which program should be used to execute the script. For example, #!/bin/bash indicates that the Bash shell should interpret the script.
Shell scripts must be compiled before they can be executed.
False. Shell scripts are interpreted at runtime and do not require compilation. They are executed line by line by the shell interpreter specified in the shebang directive, which makes them different from compiled languages like C or C++.
The chmod +x command is used to make a shell script executable.
True. The chmod +x command adds execute permission to a file, which is necessary for a shell script to be run as a program. Without this permission, the shell would treat the script as a regular text file and would not attempt to execute its contents.
(4) If you try to run a shell script without execute permissions using ./scriptname.sh, you will receive a “Permission denied” error. The operating system checks for execute permissions before attempting to run any file as a program. However, you can still execute the script by explicitly invoking a shell interpreter (like “sh scriptname.sh” or “bash scriptname.sh”), which reads the file as input rather than treating it as an executable.
(3) The incorrect statement is that shell scripts typically perform better than compiled programs. In reality, shell scripts are generally slower in execution compared to compiled programs because they are interpreted at runtime, line by line, rather than being translated to machine code beforehand. Their advantage lies in development speed and flexibility, not execution performance.
(2) The first command you should use is “chmod +x scriptname.sh” to add execute permissions to the script. A “Permission denied” error when trying to run a script with ./scriptname.sh typically indicates that the script lacks execute permissions, which must be granted before the script can be run as a program.
| Component | Description |
|---|---|
| A. Shebang (#!) | 1. Specifies the path to the interpreter |
| B. chmod +x | 2. Makes a script executable |
| C. echo | 3. Displays text on the screen |
| D. ./ | 4. Indicates the current directory path |
A-1, B-2, C-3, D-4
- Shebang (#!) specifies the path to the interpreter that should execute the script
- chmod +x is the command used to make a script executable by adding execute permissions
- echo is a command used to display text or variable values on the screen
- ./ indicates the current directory path when running a script
| Symbol | Meaning |
|---|---|
| A. r | 1. The file can be executed |
| B. w | 2. The file can be viewed |
| C. x | 3. The file can be modified |
| D. - | 4. Permission is not granted |
A-2, B-3, C-1, D-4
- r (read) means the file can be viewed
- w (write) means the file can be modified
- x (execute) means the file can be executed as a program
- (dash) means that particular permission is not granted
When a shell script with a shebang directive is executed, the following sequence occurs:
This process allows the appropriate interpreter to execute the script according to the language or shell specified in the shebang.
To create a simple “Hello World” shell script in Linux:
touch hello_world.shecho '#!/bin/bash' > hello_world.shecho 'echo Hello World' >> hello_world.shchmod +x hello_world.sh./hello_world.shThis will display “Hello World” on the terminal when executed.
The most common and recommended shebang for a Python 3 script is:
#!/usr/bin/env python3
This shebang uses the env command to locate the Python 3 interpreter in the user’s environment, making the script more portable across different systems where Python might be installed in different locations.
To check the current permissions of a file in Linux, use the ls -l command followed by the filename:
ls -l filename.sh
This will display a detailed listing that includes the file’s permissions, owner, group, size, and modification date. The permissions are shown in the leftmost column as a string like “-rwxr-xr-x”, where: