How to Run .sh File in Linux Terminal
In this article
Running a .sh file (a shell script) is a basic but essential task on Linux and macOS. This guide shows you exactly how to execute one from the terminal, including making it executable, using the correct syntax, and fixing common errors like Permission denied or bad interpreter.
What You'll Need
- A Linux or macOS system (the commands work the same on both).
- Access to a terminal window (Ctrl+Alt+T on most Linux desktops, or Terminal.app on macOS).
- The
.shfile you want to run. If you don't have one, you can create a test file with a text editor. - Basic knowledge of navigating directories with
cd(change directory).
Step-by-Step
- Open a terminal. On Linux, press
Ctrl+Alt+Tor search for “Terminal” in your applications. On macOS, pressCmd+Space, type “Terminal”, and hit Enter. - Navigate to the directory containing your .sh file. Use the
cdcommand. For example, if your file is in~/scripts, type:cd ~/scripts
If you’re not sure where the file is, you can usefind . -name "*.sh"to search from the current directory. - Verify the file exists. Run
ls -lto list files with permissions. You should see your.shfile in the output. - Check the file’s permissions. The
ls -loutput shows something like-rw-r--r--. The first character is the file type, then three sets ofrwxfor owner, group, and others. If there is noxin any set, the file is not executable. - Make the file executable (if needed). Run:
chmod +x yourfile.sh
Replaceyourfile.shwith the actual filename. This adds execute permission for everyone. You can verify withls -lagain – you should now see-rwxr-xr-xor similar. - Run the script in one of two ways:
Method A (recommended): Run it as a program:./yourfile.sh
The./tells the shell to look in the current directory. This is the most common and reliable method.
Method B: Run it with the bash interpreter explicitly:bash yourfile.sh
This works even if the file is not executable, as long as the script is written for bash. It’s useful when you don’t want to change permissions. - If the script needs to run with elevated privileges (e.g., it modifies system files), you may need to use
sudo:sudo ./yourfile.sh
Be careful – only use sudo when you trust the script and know what it does.
Common Mistakes
- Forgetting the
./prefix. Typing justyourfile.shwill usually result incommand not found, because the current directory is not in the PATH by default. - Not making the file executable. If you get
Permission denied, you forgotchmod +x. - Using the wrong interpreter. If the script starts with
#!/bin/bashbut you run it withsh, you might get errors. Usebashfor bash scripts. - Editing the file on Windows and transferring it. Windows uses CRLF line endings, while Linux expects LF. This causes
bad interpreter: /bin/bash^Merrors. - Running a script from a different directory. If the script references files relative to its own location, you need to run it from that directory or use absolute paths.
Tips
- Always start your script with a shebang line like
#!/bin/bashto specify the interpreter. - Test your script with
bash -n yourfile.shto check for syntax errors without executing it. - Use
bash -x yourfile.shto trace execution – it prints each command before running it, which is great for debugging. - Keep your scripts in a dedicated directory like
~/binand add it to your PATH to run them from anywhere. - If you’re writing a script, make sure it has the correct line endings. Use a text editor like VSCode or nano that saves with LF by default.
Troubleshooting
“Permission denied”
Run chmod +x yourfile.sh and try again. If you still get permission denied, check that the file is not on a mounted filesystem that doesn’t support execute permissions (e.g., some USB drives).
“Command not found”
You likely forgot the ./ prefix. Use ./yourfile.sh or bash yourfile.sh.
“bad interpreter: /bin/bash^M”
This is a line-ending issue. Convert the file with:sed -i 's/\r$//' yourfile.sh
Or use dos2unix yourfile.sh if installed.
“Syntax error: unexpected end of file”
This often means a missing quote or parenthesis. Check the script with bash -n yourfile.sh to find the exact line.
The script runs but does nothing.
Check if the script has any output commands. If it’s a long-running script, it might be waiting for input. Try running with bash -x to see what’s happening.
Script works when run with bash but not with ./
Make sure the shebang line is correct and the file is executable. Also, check that the interpreter path exists (e.g., /bin/bash).
Frequently Asked Questions
Do I need to make the file executable to run it?
No, you can run it with bash yourfile.sh without execute permission. However, making it executable (chmod +x) is necessary if you want to run it directly with ./yourfile.sh.
What is the difference between sh and bash?
sh (Bourne shell) is the original Unix shell, while bash (Bourne Again SHell) is an enhanced version. Many scripts are written for bash and may not work correctly with sh. Always use the interpreter specified in the shebang.
Can I run a .sh file on Windows?
Yes, you can use Windows Subsystem for Linux (WSL), Git Bash, or Cygwin to run shell scripts. However, you may need to adjust line endings and paths.
How do I run a .sh file from a different directory?
You can use an absolute path, e.g., /home/user/scripts/yourfile.sh, or navigate to the directory and run ./yourfile.sh. If the script needs to access files relative to its own location, it’s best to run it from that directory.
What does chmod +x actually do?
It adds the execute permission for the file owner, group, and others. This allows the operating system to run the file as a program.
Why do I get “No such file or directory” when running ./script.sh?
This error can occur if the file doesn’t exist, or if the shebang line points to a non-existent interpreter (e.g., #!/bin/bash but bash is in /usr/bin/bash). Check the file path and the shebang line.