TechBuzzPro

How to Run .sh File in Linux Terminal

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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 .sh file 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

  1. Open a terminal. On Linux, press Ctrl+Alt+T or search for “Terminal” in your applications. On macOS, press Cmd+Space, type “Terminal”, and hit Enter.
  2. Navigate to the directory containing your .sh file. Use the cd command. For example, if your file is in ~/scripts, type:
    cd ~/scripts
    If you’re not sure where the file is, you can use find . -name "*.sh" to search from the current directory.
  3. Verify the file exists. Run ls -l to list files with permissions. You should see your .sh file in the output.
  4. Check the file’s permissions. The ls -l output shows something like -rw-r--r--. The first character is the file type, then three sets of rwx for owner, group, and others. If there is no x in any set, the file is not executable.
  5. Make the file executable (if needed). Run:
    chmod +x yourfile.sh
    Replace yourfile.sh with the actual filename. This adds execute permission for everyone. You can verify with ls -l again – you should now see -rwxr-xr-x or similar.
  6. 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.
  7. 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 just yourfile.sh will usually result in command not found, because the current directory is not in the PATH by default.
  • Not making the file executable. If you get Permission denied, you forgot chmod +x.
  • Using the wrong interpreter. If the script starts with #!/bin/bash but you run it with sh, you might get errors. Use bash for 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^M errors.
  • 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/bash to specify the interpreter.
  • Test your script with bash -n yourfile.sh to check for syntax errors without executing it.
  • Use bash -x yourfile.sh to trace execution – it prints each command before running it, which is great for debugging.
  • Keep your scripts in a dedicated directory like ~/bin and 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.