How To Run Sh File In Linux Command Line : Execute Bash Scripts Terminal

Running an SH file from the Linux command line requires setting the correct file permissions first. This guide will show you exactly how to run sh file in linux command line with clear steps and practical examples. Whether you are a beginner or need a quick refresher, you will find everything here.

Shell scripts (files ending with .sh) are powerful tools for automating tasks in Linux. But before you can execute them, you need to understand the permission system and the right commands. Let us start with the basics and move to more advanced methods.

Understanding Sh Files And Permissions

A .sh file is a plain text file containing a series of commands for the shell interpreter. It is like a recipe for your computer to follow. However, Linux does not allow you to run a script just by double-clicking it. You must explicitly give it execute permission.

Think of permissions as a security lock. Every file has three types of permissions: read (r), write (w), and execute (x). For scripts, the execute permission is what allows the system to run them as programs.

Checking Current Permissions

Before running any script, check its permissions using the ls -l command. This shows a detailed list including permission bits.

Example output:

  • -rw-r--r-- 1 user user 1024 Jan 10 12:00 myscript.sh

Here, the first three characters -rw- mean the owner can read and write but not execute. The missing x indicates no execute permission. You need to add it.

Adding Execute Permission

To grant execute permission to the owner, use the chmod command:

  1. Open your terminal.
  2. Navigate to the directory containing your .sh file using cd.
  3. Type: chmod +x filename.sh

This adds execute permission for everyone. For more control, you can use chmod u+x filename.sh to give permission only to the file owner.

After running chmod, check permissions again with ls -l. You should now see -rwxr--r-- or similar, with the x present.

How To Run Sh File In Linux Command Line

Now that your script has execute permission, you can run it. There are several ways to do this, each with its own use case. Let us explore the most common methods.

Method 1: Using ./ Before The Filename

The simplest way is to type ./ followed by the script name. The dot-slash tells the shell to look for the file in the current directory.

Steps:

  1. Make sure you are in the same directory as the script.
  2. Type: ./myscript.sh
  3. Press Enter.

This method works because the current directory is not usually in your PATH environment variable. Without ./, the shell would search through system directories and likely fail.

Method 2: Using Bash Command

If you prefer not to change permissions, you can run the script directly with the bash interpreter:

bash myscript.sh

This works even if the file has no execute permission. The bash command reads the script and executes its contents line by line. It is useful for testing scripts without altering permissions.

Method 3: Using Sh Command

Similarly, you can use the sh command:

sh myscript.sh

This calls the default system shell, which is often bash but could be dash on some systems. It is a quick way to run scripts, especially if you are unsure about the shebang line.

Method 4: Using Source Or Dot Command

To run a script in the current shell session (instead of a subshell), use the source command or its shorthand . (dot):

  • source myscript.sh
  • . myscript.sh

This is useful when the script sets environment variables or changes the current directory. Unlike other methods, changes made by the script persist in your terminal session.

Common Issues And Solutions

Even experienced users run into problems. Here are frequent errors and how to fix them.

Permission Denied Error

If you see “Permission denied,” it means the file lacks execute permission. Run chmod +x filename.sh and try again. Alternatively, use bash filename.sh to bypass the permission check.

Command Not Found Error

This error occurs when you type the script name without ./. The shell cannot find it in the PATH. Always use ./ or specify the full path like /home/user/scripts/myscript.sh.

Bad Interpreter Error

If the script starts with a shebang line like #!/bin/bash but the interpreter path is wrong, you get this error. Check the first line of your script. For example, if bash is installed at /usr/bin/bash, update the shebang accordingly.

Syntax Errors In The Script

Syntax errors often happen due to missing quotes, wrong line endings, or invisible characters. Use a text editor with syntax highlighting, like Vim or VS Code. You can also run bash -n myscript.sh to check for syntax errors without executing.

Best Practices For Running Sh Files

Following these tips will save you time and prevent mistakes.

Always Test With Bash -N

Before running a script, validate its syntax with bash -n script.sh. This checks for errors without executing anything. It is a safety net for complex scripts.

Use Absolute Paths In Scripts

When your script references files or commands, use full paths. This avoids confusion if the script runs from a different directory. For example, use /home/user/data/file.txt instead of just file.txt.

Add Comments To Your Scripts

Comments explain what each part does. Use # to add comments. This helps you and others understand the script later. It also makes debugging easier.

Set Proper Shebang Line

The first line of your script should specify the interpreter. For bash scripts, use #!/bin/bash. For sh scripts, use #!/bin/sh. This ensures the correct shell runs your script.

Advanced Execution Techniques

Once you are comfortable with basic execution, try these advanced methods.

Running Scripts With Arguments

You can pass arguments to your script when running it:

./myscript.sh arg1 arg2 arg3

Inside the script, these arguments are accessed as $1, $2, $3, and so on. This makes scripts flexible and reusable.

Running Scripts In Background

To run a script without blocking your terminal, add an ampersand at the end:

./myscript.sh &

The script runs in the background, and you get your prompt back. Use jobs to see background processes and fg to bring one to the foreground.

Using No Hangup (Nohup)

If you want a script to keep running even after you log out, use nohup:

nohup ./myscript.sh &

Output is saved to nohup.out by default. This is ideal for long-running tasks.

Running Scripts With Sudo

Some scripts require root privileges. Use sudo before the command:

sudo ./myscript.sh

Be careful with sudo, as it gives full system access. Only run trusted scripts this way.

Frequently Asked Questions

What Is The Difference Between Bash And Sh When Running A Script?

Bash is an enhanced version of sh with more features. Running with bash uses the Bash shell, while sh uses the default system shell, which may be dash or bash in POSIX mode. For most scripts, either works, but bash-specific syntax may fail under sh.

Can I Run An Sh File Without Execute Permission?

Yes, you can use bash filename.sh or sh filename.sh to run it without execute permission. The interpreter reads the file directly. However, this bypasses the shebang line, so the script may behave differently.

Why Do I Need To Use ./ Before The Script Name?

Linux searches for executables in directories listed in the PATH variable. The current directory is usually not in PATH for security reasons. Using ./ explicitly tells the shell to look in the current directory.

How Do I Make An Sh File Executable For All Users?

Use chmod a+x filename.sh to give execute permission to everyone (owner, group, and others). Alternatively, chmod 755 filename.sh sets read and execute for all, plus write for the owner.

What Should I Do If My Script Runs But Does Nothing?

Check the script content for errors. Use bash -x myscript.sh to run it in debug mode, which prints each command before execution. This helps identify where the script fails or produces no output.

Conclusion

You now know how to run sh file in linux command line using multiple methods. Start by setting execute permission with chmod +x, then run it with ./. If you prefer not to change permissions, use bash or sh directly. For scripts that affect your environment, use source or the dot command.

Practice with simple scripts to build confidence. Always test syntax before running, and use absolute paths to avoid errors. With these skills, you can automate tasks, manage systems, and write powerful scripts in Linux.

Remember, the key to mastering the command line is experimentation. Try different methods, read error messages carefully, and consult documentation when stuck. Over time, running sh files will become second nature.