VMware ESXi and vSphere Cluster Management
How to Create and Run Shell Scripts in Linux
Learn how to create a Bash shell script, add a shebang and comments, set execute permissions, and run it with absolute or relative paths.
What Is a Shell Script?
A shell script is a text file containing commands that a shell can interpret and execute. A shell is a command-line program, and Bash is a commonly used Linux shell and scripting interpreter.
Scripts let you automate a task or group several terminal commands into one reusable file. Instead of entering each command interactively at the prompt, you can run the script and let the shell process its commands in sequence.
An interactive command is typed directly into the terminal and runs immediately. A script stores commands in a file so that they can be run again, shared, or modified later.
Create a Script File
Use a text editor to create a file. For example, you could create a file named my_script with the terminal editor nano:
nano my_scriptYou could also use a filename such as hello.sh. The .sh extension is a conventional way to indicate a shell script, but Linux does not require it. The file can be named my_script, hello.sh, or another suitable name.
Add the Shebang Interpreter Line
An executable script normally begins with a shebang. A shebang is the #! prefix at the start of the file followed by the program that should interpret the script.
#!/bin/bashIn this line, #! tells Linux that the rest of the line identifies an interpreter. The path /bin/bash identifies Bash, the program that reads and executes the script's commands.
The interpreter must match the syntax used in the script. For example, a script using Bash-specific features should select Bash rather than a different shell. Interpreter locations can vary between systems, so this portability-oriented form is also common:
#!/usr/bin/env bashThis lesson uses the direct #!/bin/bash form in its examples because it clearly shows the interpreter path.
Write Basic Script Content
After the interpreter line, add commands. The following script includes a comment and an echo command that prints visible output:
#!/bin/bash
# Display a message
echo "Hello from a shell script"When Bash runs this file, it reads the commands from top to bottom. The first line selects Bash, the second line describes what the script does, and the third line prints the message.
Comments
A comment is explanatory text that the shell ignores. In ordinary shell-script content, # begins a comment. The shell ignores text after # on a comment line or after a command, subject to the shell's parsing context.
#!/bin/bash
# This entire line is a comment
echo "Start" # This explains the commandThe first-line #! is special. Although it starts with #, it identifies the interpreter when the file is executed directly. Add comments to document a script's purpose, important assumptions, or major steps.
Save the File and Add Execute Permission
To run a script directly as a program, the file needs execute permission. Execute permission allows a file to be run as a program or script.
chmod is the command used to modify file permission modes. Add execute permission for all user classes with:
chmod a+x my_scriptIn a+x, a means all user classes—owner, group, and others—and +x adds execute permission. If only the file owner should receive execute permission, use:
chmod u+x my_scriptInspect the file's permissions with:
ls -l my_scriptThe output contains permission fields near the beginning. An x in those fields indicates execute permission for the corresponding user class.
| Command | Permission effect | Typical use |
|---|---|---|
chmod a+x my_script | Adds execute permission for the owner, group, and others | Make the script executable for all user classes |
chmod u+x my_script | Adds execute permission only for the owner | Use a more restrictive permission change |
ls -l my_script | Does not change permissions | Inspect the file's permission bits |
Run a Shell Script
A script is launched by providing its path. A path identifies a file's location in the filesystem. You can use an absolute path, a relative path, or explicitly ask Bash to interpret the file.
Run with an Absolute Path
An absolute path is a full filesystem location beginning at the root directory, represented by /. For example:
/home/username/my_scriptReplace /home/username/my_script with the actual location of your file. An absolute path works regardless of which directory is your current working directory, provided the path is correct and the file is executable.
Run from the Current Directory
A relative path is interpreted from the terminal's current working directory—the directory in which the terminal session is presently operating. The relative-path prefix ./ means “the current directory.”
./my_scriptIf the script is in the current directory and has execute permission, this directly executes it using the interpreter specified by its shebang.
Typing only my_script usually does not run a file in the current directory. Linux commonly omits the current directory from PATH. PATH is an environment variable containing directories that the shell searches for command names. Using ./my_script makes the location explicit and avoids relying on the current directory being in PATH.
Invoke Bash Explicitly
You can ask Bash to read the file directly:
bash my_scriptThis can run a readable script even when the file does not have execute permission. However, it does not test the direct executable-script setup: the execute bit and shebang are not needed in the same way because you explicitly selected Bash. For a script intended to be launched as a program, use the shebang, set execute permission, and run it by path.
| Method | Example | When it works | Key point |
|---|---|---|---|
| Absolute path | /home/username/my_script | The path is correct and the file is executable | Uses the script's complete filesystem location |
Relative path using ./ | ./my_script | The script is in the current directory and is executable | ./ means the current working directory |
| Explicit Bash invocation | bash my_script | Bash can read the file | Runs through Bash without testing direct execution permissions |
Complete Beginner Example
Create and edit the file:
nano my_scriptEnter this content:
#!/bin/bash
# Display a message
echo "Hello from a shell script"Save the file, exit the editor, and add execute permission:
chmod u+x my_script
ls -l my_scriptRun it from the directory containing the file:
./my_scriptThe output should be:
Hello from a shell scriptTroubleshooting
Permission denied with ./my_script
The file probably lacks execute permission. Check it with:
ls -l my_scriptAdd permission with chmod a+x my_script or the more restrictive chmod u+x my_script, then try ./my_script again.
Command not found after typing my_script
The current directory is commonly not included in PATH. Confirm that the file is in the current directory, then use:
./my_scriptYou can also use the script's absolute path.
No such file or directory
The command may contain an incorrect path, or the terminal may be in a different directory from the script. Use pwd to display the current working directory and ls to list its files:
pwd
lsChange to the correct directory with cd, or provide the correct relative or absolute path.
Unexpected Bash syntax behavior
Inspect the first line and identify the syntax used by the script. If the script requires Bash, use #!/bin/bash and execute it directly, or invoke it explicitly with bash my_script. An unintended interpreter can interpret shell-specific syntax differently.
Interpreter-related error on the first line
Check whether the interpreter path in the shebang exists on the system. Also check for Windows-style CRLF line endings if the file was edited on Windows. Convert the file to Unix LF line endings and use a valid interpreter path, such as /bin/bash or the appropriate /usr/bin/env bash form.
Key Points to Remember
- A shell script is a text file containing commands interpreted by a shell.
- The shebang
#!/bin/bashselects Bash for direct execution. - The
.shextension is conventional, not mandatory. - Use
chmodto add execute permission before direct execution. - Use an absolute path or
./my_scriptto identify the script location. - The current directory is commonly absent from
PATH, so typing only the filename usually does not work. bash my_scriptexplicitly invokes Bash and can run a readable file without the execute bit.
After this foundation, useful next topics include Bash variables and quoting, command-line arguments, exit statuses, conditionals, loops, redirection, functions, and debugging with bash -n and bash -x.