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_script

You 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/bash

In 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 bash

This 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 command

The 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_script

In 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_script

Inspect the file's permissions with:

ls -l my_script

The output contains permission fields near the beginning. An x in those fields indicates execute permission for the corresponding user class.

CommandPermission effectTypical use
chmod a+x my_scriptAdds execute permission for the owner, group, and othersMake the script executable for all user classes
chmod u+x my_scriptAdds execute permission only for the ownerUse a more restrictive permission change
ls -l my_scriptDoes not change permissionsInspect 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_script

Replace /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_script

If 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_script

This 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.

MethodExampleWhen it worksKey point
Absolute path/home/username/my_scriptThe path is correct and the file is executableUses the script's complete filesystem location
Relative path using ././my_scriptThe script is in the current directory and is executable./ means the current working directory
Explicit Bash invocationbash my_scriptBash can read the fileRuns through Bash without testing direct execution permissions

Complete Beginner Example

Create and edit the file:

nano my_script

Enter 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_script

Run it from the directory containing the file:

./my_script

The output should be:

Hello from a shell script

Troubleshooting

Permission denied with ./my_script

The file probably lacks execute permission. Check it with:

ls -l my_script

Add 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_script

You 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
ls

Change 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/bash selects Bash for direct execution.
  • The .sh extension is conventional, not mandatory.
  • Use chmod to add execute permission before direct execution.
  • Use an absolute path or ./my_script to identify the script location.
  • The current directory is commonly absent from PATH, so typing only the filename usually does not work.
  • bash my_script explicitly 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.