Create and Run Shell Scripts in Linux
Learn how to create a Bash shell script, add a shebang and commands, set executable permissions, and run it with relative, absolute, or Bash paths.
A shell script is a plain-text file containing commands interpreted by a shell. It lets you automate command-line tasks and group several commands into a reusable program.
Bash (Bourne Again Shell) is a commonly used Linux command interpreter and scripting shell. This lesson uses Bash scripts and assumes you know how to navigate directories, use a text editor, and run basic terminal commands.
Create a Script File
Use a text editor to create a file named my_script or my_script.sh. For example, you could open a file named my_script with a terminal editor:
nano my_script
The .sh extension is optional on Linux. Linux does not decide whether a file can run based on its extension. The file's contents, interpreter declaration, and executable permission determine how it is used.
For this lesson, create a file named my_script in a directory where you can easily find it.
Add the Shebang Line
A shebang is the #! prefix at the beginning of a script followed by the interpreter to use. For a Bash script, use:
#!/bin/bash
The #! tells the Linux kernel that the file is an interpreter-driven script. The path /bin/bash identifies Bash as the interpreter—the program that reads and executes the script's commands.
The shebang must be the first line. Do not place a blank line, spaces, or other text before it. When you execute the file directly, the kernel uses this line to invoke Bash.
Write Basic Commands and Comments
Commands in a script are processed in sequence by the selected shell. Start with an echo command so the script produces visible output:
#!/bin/bash
# Print a confirmation message
echo "Hello from my script"
In ordinary shell-script code, # begins a comment. Text after the marker is ignored by the shell. Comments document the purpose of a script or explain a command. The initial #! line is the exception: at the beginning of a script, it has special shebang meaning rather than being an ordinary comment.
Save the file after entering the contents. Your complete first script should contain:
| Component | Example | Purpose |
|---|---|---|
| Shebang | #!/bin/bash | Specifies Bash as the interpreter for direct execution. |
| Comment | # Print a confirmation message | Documents the script; the shell ignores it. |
| Shell command | echo "Hello from my script" | Prints a message when Bash processes the command. |
| Execute permission | Added with chmod a+x my_script | Allows the file to be run directly as a program. |
Grant Executable Permission
Linux file permissions control whether a file can be run directly. Use chmod to modify permission bits:
chmod a+x my_script
Here, a means all user classes— the owner, group, and other users. The operator +x adds execute permission. Direct execution requires this permission.
Check the result with ls -l:
ls -l my_script
Look for x characters in the permission section of the output. A result similar to -rwxr-xr-x indicates that execute permission is present for the listed user classes.
Run the Script from the Current Directory
The current working directory is the directory in which the shell is currently operating. Confirm it with:
pwd
If my_script is in that directory and has execute permission, run it with the relative path:
./my_script
./ is a relative-path prefix meaning “the current directory.” The command should print:
Hello from my script
Entering only my_script usually does not run a file from the current directory. When a command has no path, the shell searches the directories listed in the PATH environment variable. For security reasons, the current directory is commonly not included in PATH. Use ./my_script or provide another path instead.
Use an Absolute Path
An absolute path is a complete filesystem location beginning at the root directory, /. For example:
/home/user/scripts/my_script
This command can run the script from any current working directory, provided the file exists at that location and is executable. Replace /home/user/scripts/my_script with the actual full path to your file.
A relative path is interpreted from the current working directory. Examples include ./my_script and scripts/my_script.
Run the File Through Bash
You can ask Bash to read and execute the file explicitly:
bash my_script
This method does not depend on the file's executable bit in the same way as direct execution. Bash is already the program being invoked, so it reads the script as its input. The file should still contain the correct shebang when it is intended to be run directly, and the shebang should remain the first line.
| Method | Example | When to use it | Notes |
|---|---|---|---|
| Relative path from the current directory | ./my_script | The script is in the current working directory. | Requires execute permission and a valid shebang for direct execution. |
| Absolute path | /home/user/scripts/my_script | You want to run the script from any directory. | Requires the complete location, execute permission, and a valid shebang. |
| Explicit Bash invocation | bash my_script | You want Bash to read the file explicitly. | Does not rely on the executable bit in the same way as direct execution. |
Basic Shell-Script Workflow
- Create a plain-text file such as
my_scriptwith a text editor. - Put
#!/bin/bashon the first line. - Add commands such as
echoand comments that explain the script. - Save the file.
- Add execute permission with
chmod a+x my_script. - Run it with
./my_scriptfrom its directory or with its absolute path. - Verify the output and revise the file as needed.
A complete terminal sequence might look like this:
nano my_script
chmod a+x my_script
ls -l my_script
./my_script
Troubleshoot Common Problems
Permission denied
If ./my_script reports Permission denied, the file probably lacks execute permission. Add it and try again:
chmod a+x my_script
./my_script
Command not found
If entering my_script reports Command not found, the current directory or the script's directory is probably not in PATH. Run a script in the current directory with:
./my_script
Alternatively, use its absolute path.
Bash syntax fails or the wrong shell is used
If Bash-specific syntax fails during direct execution, check that #!/bin/bash is the first line and that it was saved correctly. A missing, misplaced, or different interpreter line can cause the script to be processed by an unexpected shell.
The script works with Bash but not with direct execution
If bash my_script works but ./my_script does not, check three things: execute permission, the first line's shebang, and the file's line endings. A non-Linux editor can add incompatible line endings. Convert the file to Unix line endings if necessary, then verify the shebang and permissions again.
Unexpected behavior around a number sign
Outside the special initial shebang, # starts a comment in ordinary shell code. Put executable commands before the comment marker and use the rest of the line only for explanatory text. If a literal number sign is needed as data, use appropriate shell quoting and syntax rather than assuming every context treats it identically.
Key Terms
- Shell script: A plain-text file containing shell commands.
- Bash: A commonly used Linux command interpreter and scripting shell.
- Shebang: The
#!prefix at the start of a script that specifies its interpreter. - Interpreter: The program that reads and executes script commands.
- Linux kernel: The operating-system component that handles direct execution of a script with a valid shebang.
- Executable permission: A permission that allows a file to be run directly as a program.
- chmod: The command used to modify file permission bits.
- Absolute path: A complete filesystem location beginning at
/. - Relative path: A location interpreted from the current working directory.
- PATH: An environment variable containing directories searched for commands entered without a path.
- Comment: Explanatory text ignored by the shell interpreter.
Next Bash Topics
After creating and running a basic script, useful next topics include Bash variables and quoting, command-line arguments, conditional statements, loops, functions, exit statuses, error handling, environment variables, and scheduling scripts with cron. See Bourne Again Shell Bash and Show the Full Path of Shell Commands for related Linux concepts.