How to Run a Bash Script in Linux
If you use Linux, learning to run bash scripts is an important skill. Bash scripts help you automate tasks, manage your system, and make your work easier. There are a few ways to run a bash script, and the best method depends on what you need.
This article explains the different methods for running bash scripts in Linux and how to resolve common issues. If you’re new to Linux, start with our guide on basic Linux commands .
Making a Script Executable
The most common way to run a bash script is to make it executable and run it directly.
First, at the top of your script, add a shebang line that tells the system which interpreter to use:
script.sh
Copy
#!/bin/bash
echo "Hello, World!"
The shebang must be on the first line of the file.
Use chmod to make the script executable:
Terminal
Copy
chmod +x script.sh
Now run the script by typing ./ followed by the script name:
Terminal
Copy
./script.sh
output
Copy
Hello, World!
The ./ tells the shell to look for the script in the current directory.
Running a Script with the Bash Interpreter
You can run a script by explicitly calling the bash interpreter. This method doesn’t require the script to be executable:
Terminal
Copy
bash script.sh
This is handy if you can’t change file permissions or just want to test a script quickly.
You can also use sh for POSIX-compatible scripts:
Terminal
Copy
sh script.sh
Sourcing a Script (Run in the Current Shell)
Sourcing a script runs it in the current shell session, not in a subshell. Use the source command or its shorthand .:
Terminal
Copy
source script.sh
Or:
Terminal
Copy
. script.sh
This is useful when the script sets environment variables or defines functions that you want to use in your current shell.
For example, if you have a script that sets a variable:
setenv.sh
Copy
#!/bin/bash
export MY_VAR="Hello"
When running normally, the variable is not available in the current shell:
Terminal
Copy
./setenv.sh
echo $MY_VAR
output
Copy
When sourcing, the variable persists:
Terminal
Copy
source setenv.sh
echo $MY_VAR
output
Copy
Hello
Running Scripts in the Background
To run a script in the background , append & to the command:
Terminal
Copy
./script.sh &
For long-running scripts that should continue after you log out, use nohup :
Terminal
Copy
nohup ./script.sh &
Running Scripts with Debug Mode
To debug a script and see each command before it executes, use the -x flag:
Terminal
Copy
bash -x script.sh
output
Copy
+ echo 'Hello, World!'
Hello, World!
Other useful Bash flags include:
bash -n script.sh- Check syntax without runningbash -v script.sh- Print lines as they are readbash -e script.sh- Exit immediately on error
Running Scripts from Anywhere
To run a script from any directory, you can either specify the full path or add the script to a directory in your $PATH .
A common approach is to create a ~/bin directory:
Terminal
Copy
mkdir -p ~/bin
Add it to your ~/.bashrc:
Terminal
Copy
export PATH="$HOME/bin:$PATH"
Move your script there and make it executable:
Terminal
Copy
mv script.sh ~/bin/
chmod +x ~/bin/script.sh
After reloading your shell or running source ~/.bashrc, you can run the script from anywhere:
Terminal
Copy
script.sh
Using /usr/bin/env in the Shebang (Best Practice)
Instead of hardcoding the Bash path, you can use:
script.sh
Copy
#!/usr/bin/env bash
echo "Hello, World!"
This approach is considered best practice because it works across different systems and respects the user’s environment.
Common Errors
Permission Denied
output
Copy
bash: ./script.sh: Permission denied
Make the script executable:
Terminal
Copy
chmod +x script.sh
Command Not Found
output
Copy
bash: script.sh: command not found
Use ./ prefix or provide the full path:
Terminal
Copy
./script.sh
Bad Interpreter
output
Copy
bash: ./script.sh: /bin/bash^M: bad interpreter
The file has Windows line endings. Convert it using:
Terminal
Copy
sed -i 's/\r$//' script.sh
Security Considerations
- If you downloaded a script from the Internet, always check its contents before running it.
- Be extra careful when running scripts as root.
- Use restrictive permissions when you can.
- Don’t add the current directory
(.)to$PATH. - Don’t run a script unless you’re sure it’s safe.
Conclusion
There are several ways to run bash scripts in Linux. The most common is to make the script executable with chmod +x and run it using ./script.sh. You can also run scripts with the bash interpreter or source them if you want to change your current shell.
If you have questions or feedback, please leave a comment.
Tags:bashlinux-commands
Bash Scripting Fundamentals
Part 2 of 37
- 1Bash Shebang
-
2How to Run a Bash Script in LinuxCurrent
- 3Writing Comments in Bash Scripts
- 4.bashrc vs .bash_profile
- 6Echo Command in Linux with Examples
- 7Bash printf Command
- 8Bash Concatenate String Variables
- 9How to Increment and Decrement Variable in Bash (Counter)
- 10How to Set and List Environment Variables in Linux
- 11How to Use the Export Command in Linux
- 12How to Add a Directory to PATH in Linux
- 13Bash Arrays
- 14Bash read Command
- 15Bash Heredoc
- 16Bash: Write to File
- 17Bash: Append to File
- 18How to Redirect stderr to stdout in Bash
- 19Bash if...else Statement
- 20Bash Case Statement
- 21Bash Comparison Operators
- 22How to Compare Strings in Bash
- 23How to Check if a String Contains a Substring in Bash
- 24How to Check if a File or Directory Exists in Bash
- 25Bash Sequence Expression (Range)
- 26Bash For Loop: Syntax and Examples
- 27Bash while Loop
- 28Bash until Loop
- 29Bash break and continue
- 30Bash Select (Make Menus)
- 31Bash Functions
- 32Bash Source Command
- 33Bash Exit Command and Exit Codes
- 34Bash wait Command
- 35Linux Sleep Command (Pause a Bash Script)
- 36How to Read a File Line By Line in Bash
- 37How to Create Bash Aliases
- 38History Command in Linux (Bash History)
Previous in seriesNext in series
More Like This
更多推荐


所有评论(0)