Linux Shell Scripts: What They Are and How They Work
Learn what Linux shell scripts are, how Bash interprets them, and how to create, execute, and troubleshoot simple scripts.
A shell script is a plaintext program containing commands and shell-language instructions. A Linux shell reads those instructions and executes them. Shell scripts are useful for combining routine command-line work into a repeatable task.
This lesson assumes that you can enter commands in a terminal, work with files and directories, and understand basic Linux permissions. For background, see Linux file structure and Linux file ownership and permissions.
What Is a Shell Script?
A shell is a command interpreter: a program that accepts commands and runs them. Bash is one example of a shell. A command is an instruction executed by the shell, such as pwd, mkdir, or cp.
A shell script is a readable plaintext file containing several commands and other shell-language constructs. Unlike a compiled binary executable, it is normally read and interpreted when you run it.
When you type a command interactively, the shell receives and executes that command immediately. With a script, you save a sequence of instructions in a file and ask the shell to interpret that file. The result is a reusable command-line program.
Why Shell Scripts Are Useful
- Automation: one script invocation can replace repeatedly typing the same sequence of commands.
- Repeatability: the same instructions can be run again with fewer opportunities for typing mistakes.
- Organization: related administrative or user tasks can be kept together in one readable file.
- Consistency: a documented script makes it clearer which commands are used and in what order.
For example, a routine task might create a directory, write a status message, and list the directory contents. You could type those commands manually each time, or save them in a script and run the script whenever needed.
Shell Scripting Languages
Shell scripts are written for a particular shell. The shell that runs the file provides the syntax and features used by the script, so scripts written for different shells are not always interchangeable.
In the usual shell-script workflow, an interpreter reads and executes the script instructions. The script is not first compiled into a standalone binary in the way a C or Rust program commonly is.
Core Parts of a Shell Script
Even a small script can contain several kinds of elements. Commands perform work, variables hold values, functions organize reusable instructions, and comments explain the file.
Commands
Commands usually run in the order in which they appear. A script can invoke built-in shell operations, external Linux commands, or other programs available on the system.
Variables
A variable is a named value. In Bash, assignment has no spaces around the equals sign. Prefix a variable with $ when expanding its value:
name="Ada"
echo "Hello, $name"
The assignment stores the text Ada in name. The echo command then uses the expanded value.
Functions
A function is a named reusable group of instructions. Define it once and invoke its name whenever the group is needed:
show_message() {
echo "A routine task is running"
}
show_message
Comments and Readable Structure
In Bash, a comment begins with # and continues to the end of the line. Use comments to explain the purpose of a section or a non-obvious command. Keep related commands together, use meaningful variable and function names, and indent function bodies consistently.
Creating and Running a Bash Script
- Create a plaintext file, for example with
touch task.sh, and open it in an editor such asnano task.sh. - Put the intended interpreter declaration on the first line.
- Add commands and any variables, functions, or comments needed by the task.
- Run the file explicitly with Bash, or grant executable permission and run it directly.
touch task.sh
nano task.sh
For a Bash script, the first line is commonly:
#!/usr/bin/env bash
This line is called a shebang. It tells the operating system which interpreter should be used when the file is executed directly.
Run Through Bash
You can invoke a script through Bash without changing its executable permission:
bash task.sh
This explicitly selects Bash for this invocation. It is useful when testing a script or when the file is not executable.
Run Directly as a Command
To run the file directly, grant it executable permission:
chmod +x task.sh
./task.sh
The ./ means “the file named task.sh in the current directory.” Linux generally does not search the current directory automatically when you type only task.sh.
Example: Run Several Commands with One Script
Create task.sh with the following Bash content:
#!/usr/bin/env bash
# Perform a small routine task
mkdir -p reports
echo "Reports directory:"
pwd
ls reports
Line-by-Line Explanation
#!/usr/bin/env bashselects Bash when the file is run directly.- The blank line improves readability and has no effect on execution.
# Perform a small routine taskis a comment for the reader.mkdir -p reportscreates thereportsdirectory if it does not already exist.echo "Reports directory:"prints a label.pwdprints the current working directory.ls reportslists the contents of the new directory.
Run it with either method:
bash task.sh
Or:
chmod +x task.sh
./task.sh
Bash processes the commands in sequence. One invocation performs the same group of actions that would otherwise require entering each command manually.
Comparing Manual Entry with Script Execution
Without a script, you might enter the following commands every time:
mkdir -p reports
echo "Reports directory:"
pwd
ls reports
With a saved script, the repeated task becomes:
./task.sh
The script does not eliminate the need to understand the commands. It records the sequence so that it can be reused consistently and reviewed later.
Variables and Functions Together
Variables and functions make a script easier to adapt and organize:
#!/usr/bin/env bash
directory="reports"
show_directory() {
echo "Contents of $directory:"
ls "$directory"
}
mkdir -p "$directory"
show_directory
The directory variable stores the directory name. The function show_directory uses that value and groups the display commands. Quoting "$directory" is a good habit because it protects values containing spaces.
Troubleshooting Common Problems
The Script Will Not Run Directly
- The file may not have executable permission. Use
chmod +x task.sh. - You may be omitting the path. From the current directory, use
./task.sh; otherwise use an appropriate relative or absolute path.
The Wrong Shell Interprets the Script
- The file may lack a shebang. Add an appropriate first line, such as
#!/usr/bin/env bash. - The file may contain Bash syntax but be started by another shell. Run it explicitly with
bash task.shor use the correct interpreter declaration.
A Command Works Interactively but Fails in the Script
- The script may have a different working directory or environment from your interactive terminal.
- A command may not be available through the script's
PATH, or a relative file path may point somewhere unexpected. - Check the current directory and environment assumptions, and use explicit paths where needed.
Key Points
- A shell script is a plaintext program interpreted by a shell.
- Bash is a common Linux shell and a common target for shell scripts.
- Scripts combine commands into reusable, repeatable tasks.
- Variables store values, functions organize reusable instructions, and comments improve readability.
- A shebang identifies the intended interpreter for direct execution.
- You can run a script with
bash script-nameor make it executable and run it with a path such as./script-name. - Shell syntax differs between Bash, ksh, tcsh, and other shells, so target the interpreter deliberately.
Next, build on these foundations with Bash syntax and features, finding the full path of commands, and searching text with grep.