How Run a Shell Script in Linux

How Run a Shell Script in Linux

To how run a shell script in linux, open a terminal, go to the script’s folder, and run it with a shell such as bash script.sh or make it executable with chmod +x script.sh and run ./script.sh. The best method depends on whether the script already has execute permission and whether its shebang line points to the right interpreter.

Quick answer

If you just want the fastest way to run a shell script in Linux, use one of these commands:

bash script.sh
sh script.sh
./script.sh

Use bash script.sh when you want Bash to interpret the file, sh script.sh when the script is written for POSIX sh, and ./script.sh when the file is executable and has a valid shebang such as #!/bin/bash or #!/usr/bin/env bash.

What you need before running a shell script

What you need before running a shell script

A shell script is just a text file that contains commands for a shell to execute. Before you run one, check these basics:

  • The file exists in the directory you expect.
  • You know which shell it needs, such as Bash or sh.
  • The file has the right permissions if you plan to run it directly.
  • The script has a correct shebang line if you want to launch it with ./script.sh.

You can confirm the file is there with:

ls -l

If you are not in the same directory as the script, use the full path or change into the script’s directory first:

cd /path/to/script

Run the script with bash or sh

One of the simplest ways to run a shell script in Linux is to pass it directly to the shell interpreter. This does not require execute permission on the file.

Using Bash

If the script is written for Bash, run:

bash script.sh

This is often the safest choice when the script uses Bash-specific features such as arrays, [[ ... ]], or certain parameter expansions.

Using sh

If the script is meant for a POSIX-compatible shell, run:

sh script.sh

Use sh only if the script is compatible with it. Some scripts that work in Bash will fail in sh because the syntax is different.

When this method is useful

  • You do not want to change file permissions.
  • You are testing a script quickly.
  • You want to force a specific interpreter to run the script.

Make the script executable and run it directly

Make the script executable and run it directly

If you want to launch the script like a program, give it execute permission first:

chmod +x script.sh

Then run it with:

./script.sh

The ./ matters. It tells Linux to run the file from the current directory, rather than searching system command paths like /usr/bin or /bin.

Why the shebang line matters

When you run a script directly, Linux uses the first line of the file to decide which interpreter to start. That line is called the shebang. Common examples are:

#!/bin/bash
#!/usr/bin/env bash
#!/bin/sh

Without a valid shebang, running ./script.sh may fail or use the wrong interpreter. If the script depends on Bash features, the shebang should point to Bash.

Example workflow

cd ~/scripts
chmod +x backup.sh
./backup.sh

This is the normal approach when you want a script to behave like a standalone command.

Which method should you use?

MethodCommandWhat it doesWhen to use it
Run with Bashbash script.shBash interprets the file directlyScript is written for Bash or you want to test with Bash explicitly
Run with shsh script.shPOSIX shell interprets the fileScript is portable and compatible with sh
Run directly./script.shExecutes the file as a program using its shebangScript has execute permission and a correct interpreter line

A simple rule helps:

  • Use bash script.sh if the script is Bash-specific or you are unsure but know it requires Bash.
  • Use sh script.sh only for scripts meant to be portable.
  • Use ./script.sh if the script is executable and you want the shebang to choose the interpreter.

Common errors and how to fix them

Permission denied

If you see Permission denied when using ./script.sh, the file probably is not executable. Fix it with:

chmod +x script.sh

Then try again with ./script.sh.

If you are using a mounted drive or a restricted folder, file system permissions can also block execution.

No such file or directory

This error usually means Linux cannot find the file or the interpreter named in the shebang.

Check the path first:

pwd
ls -l script.sh

If the script is in another directory, use the full path:

/home/yourname/scripts/script.sh

If the script exists but the shebang points to a missing interpreter, correct the first line of the file.

Bad interpreter

A bad interpreter message often means the shebang line is wrong or the interpreter path does not exist. For example, #!/bin/bash assumes Bash is installed at that location. On most Linux systems it is, but not always on minimal installations or inside special environments.

You can check where Bash is installed with:

which bash

Then update the shebang if needed, or run the script with bash script.sh.

Script does nothing or errors on line endings

Script does nothing or errors on line endings

If a script was edited on Windows, it may have Windows line endings, which can cause strange errors in Linux. A common fix is to convert it to Unix line endings with a tool such as dos2unix if it is available.

dos2unix script.sh

After conversion, try running the script again.

Command not found

If a command inside the script is missing, the script itself may run, but one of its steps fails. In that case, install the missing tool or adjust the script so it uses commands available on your system.

How to check the current directory and script path

How to check the current directory and script path

Many execution problems come from running the command in the wrong folder. These commands help:

pwd
ls
ls -l script.sh

pwd shows your current directory. ls shows the files there. ls -l shows permissions, ownership, and whether the script is executable.

You can also use tab completion in the terminal to reduce typing mistakes when entering file names.

Safety checks before running an unknown script

Shell scripts can change files, install software, or run other commands on your system. Before you execute a script you downloaded or received from someone else, review it first.

  • Open the file in a text editor and read the commands.
  • Check the shebang so you know which shell will run it.
  • Look for destructive commands such as rm -rf, file moves, or package installs.
  • Confirm the source is trustworthy before you run it.
  • Prefer running it with an explicit shell if you only want to inspect behavior, such as bash -x script.sh for tracing.

If you want a dry run or more visibility, bash -x script.sh can help by printing each command as it runs. That makes troubleshooting easier, but it still executes the script, so use it carefully.

Practical examples

Here are a few common ways to run a shell script in Linux:

# Run with Bash
bash deploy.sh

# Run with sh
sh install.sh

# Make executable and run directly
chmod +x cleanup.sh
./cleanup.sh

# Run from another directory with a full path
bash /home/user/scripts/backup.sh

If the script is in your current directory, remember that ./ is required for direct execution. Typing only the file name usually will not work unless the directory is already in your PATH.

When to use bash script.sh versus ./script.sh

This is one of the most useful distinctions to understand:

  • bash script.sh ignores the executable bit and uses Bash directly.
  • ./script.sh requires execute permission and uses the interpreter named in the shebang.

If you are debugging or testing, bash script.sh is often simpler. If you are packaging a script for regular use, ./script.sh is cleaner and more flexible because the shebang controls execution.

Frequently Asked Questions About how run a shell script in linux

How do I run a shell script in Linux from the terminal?

Use bash script.sh, sh script.sh, or make the file executable with chmod +x script.sh and run ./script.sh.

How do I make a shell script executable?

Run chmod +x script.sh. After that, you can execute it with ./script.sh if the script has a valid shebang.

Why do I get permission denied when running a script?

Usually because the file does not have execute permission. Fix it with chmod +x script.sh. In some cases, the file system or folder permissions can also block execution.

Can I run a shell script without chmod?

Yes. You can run it with bash script.sh or sh script.sh without making it executable, because the shell reads the file directly.

What is the difference between bash script and sh script?

bash script.sh uses Bash features, while sh script.sh uses a more limited POSIX shell. A script written for Bash may not work under sh.

Why does ./script.sh not work?

Common reasons include missing execute permission, a wrong current directory, a bad shebang, or Windows line endings in the file.

Do I need to be in the same folder as the script?

No. You can use a relative path like ./script.sh from the same directory, or an absolute path like /home/user/scripts/script.sh from anywhere.

Conclusion

The easiest way to run a shell script in Linux is to use bash script.sh or, if the file is executable, ./script.sh. Use chmod +x to add execute permission, make sure the shebang points to the right interpreter, and check the path if you get a file-not-found error. Once you understand the difference between running a script through a shell and executing it directly, Linux script execution becomes straightforward.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *