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 several ways to execute a bash script, and the best method depends on what you need.
This article explains the different methods for running and executing bash scripts in Linux and how to resolve common issues. If you are new to Linux, start with our guide on basic Linux commands .
Quick Reference
For a printable quick reference, see the Bash cheatsheet .
| Method | Command | When to use |
|---|---|---|
| Executable | ./script.sh | Standard run after chmod +x |
| Bash interpreter | bash script.sh | No permission change needed |
| Source | source script.sh or . script.sh | Apply variables and functions to current shell |
| Background | ./script.sh & or nohup ./script.sh & | Long-running tasks |
| Debug | bash -x script.sh | Trace each command before it runs |
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:
#!/bin/bash
echo "Hello, World!"Use chmod
to make the script executable:
chmod +x script.shNow run the script by typing ./ followed by the script name:
./script.shHello, 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 does not require the script to be executable:
bash script.shThis is handy if you cannot change file permissions or want to test a script quickly.
You can also use sh for POSIX-compatible scripts:
sh script.shSourcing 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 .:
source script.shOr:
. script.shThis 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:
#!/bin/bash
export MY_VAR="Hello"When running normally, the variable is not available in the current shell:
./setenv.sh
echo $MY_VARThe command prints no output because the variable is set only inside the child process.
When sourcing, the variable persists:
source setenv.sh
echo $MY_VARHelloRunning Scripts in the Background
To run a script in the background
, append & to the command:
./script.sh &For long-running scripts that should continue after you log out, use nohup
:
nohup ./script.sh &Running Scripts with Debug Mode
To debug a script and see each command before it executes, use the -x flag:
bash -x script.sh+ 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:
mkdir -p ~/binAdd it to your ~/.bashrc:
export PATH="$HOME/bin:$PATH"Move your script there and make it executable:
mv script.sh ~/bin/
chmod +x ~/bin/script.shAfter reloading your shell or running source ~/.bashrc, you can run the script from anywhere:
script.shUsing /usr/bin/env in the Shebang
Instead of hardcoding the Bash path, you can use:
#!/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.
Troubleshooting
Permission Denied
bash: ./script.sh: Permission deniedMake the script executable:
chmod +x script.shCommand Not Found
bash: script.sh: command not foundUse ./ prefix or provide the full path:
./script.shBad Interpreter
bash: ./script.sh: /bin/bash^M: bad interpreterThe file has Windows line endings. Convert it using:
sed -i 's/\r$//' script.shSecurity 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.
- Do not add the current directory
(.)to$PATH. - Do not run a script unless you are sure it is safe.
Conclusion
A practical workflow is to keep your scripts in ~/bin, make them executable with chmod +x, and start each one with #!/usr/bin/env bash. Reach for source only when a script needs to set variables or define functions in your current shell.
Next, learn how to write your own with our guide on bash functions .
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page