How to Run sh File in Linux
(How to Run .sh File in Linux) The Linux environment uses .sh files or Shell scripts extensively for task automation command sequence execution and system process management. Knowledge of executing .sh files becomes necessary for any user who wants to accomplish software installation automation or development through terminal commands. This tutorial explains multiple techniques for running .sh files on Linux platforms through terminal access and file executable modification and error handling steps.
What is a .sh
File?
A shell script written in .sh format functions as a text document that executes sequential commands under Bash (#!/bin/bash) or Zsh (#!/bin/zsh) command line interpreters. Linux users can execute several commands through these scripts which minimize the need for manual user interaction.
Example of a Simple Shell Script (myscript.sh
)
Now, let’s explore how to run a .sh
file in Linux.
Checking If You Have an .sh
File
Before running a shell script, navigate to the directory where the .sh
file is located. Use the following command to check:
Example Output:
If the file is not present, you may need to create it using:
Then, add script content and save using Ctrl + X, Y, and Enter.
Running a .sh
File in Linux
Method 1: Using sh
or bash
Command (Without Execution Permission)
You can run a shell script without modifying its permissions using:
or
Example Output:
💡 Tip: This method works even if the script file is not marked as executable.
Method 2: Making the Script Executable (chmod +x
)
To run a script as an executable program, you must grant it execute permissions.
Step 1: Give Execute Permission
Step 2: Run the Script
Example Output:
💡 Why Use chmod +x
?
By default, new files are not executable. Running chmod +x
ensures the script can be executed directly.
4. Running a .sh
File with Different Shells
Linux supports multiple shells like Bash, Zsh, Ksh, and Dash. You can explicitly specify which shell to use:
Run with Bash:
Run with Zsh:
Run with Ksh:
5. Running a .sh
File as Root (Superuser Mode)
Some scripts require administrative privileges (e.g., installing software). To execute a script with root privileges, use:
Example Output:
💡 Tip: Be cautious when running scripts as root, as they can modify system files.
6. Running a .sh
File in the Background
To run a script in the background (useful for long-running tasks), append &
at the end:
To check running background processes, use:
To bring a background job to the foreground:
7. Running a .sh
File at Startup
If you want to execute a script automatically when the system boots, add it to crontab or /etc/rc.local
.
Using crontab
:
Then, add:
Using rc.local
(For Older Linux Systems):
Edit /etc/rc.local
:
Add:
Ensure the script is executable:
Troubleshooting Common Errors
Error: “Permission Denied”
Solution:
Error: “Command Not Found”
Solution:
Make sure you’re in the correct directory:
Then run:
Error: “No Such File or Directory”
Solution:
Check the script file name using:
If it doesn’t exist, create it using:
9. Running a .sh
File in a GUI-Based Linux Environment
If you prefer not to use the terminal, you can run shell scripts by:
- Right-click the script file → Select Properties
- Go to the “Permissions” tab”
- Enable “Allow executing file as a program”
- Double-click the script and choose “Run in Terminal”
10. Running a .sh
File in Another Script
You can call one script from another using:
Or specify the full path:
Conclusion
Running .sh files through Linux requires three methods: sh, bash and chmod +x file permission. You can execute root privilege scripts while running them in the background or implementing them for system startup. This guide included various execution methods together with troubleshooting strategies and automated script running procedures. Your knowledge of executing shell scripts in Linux now reaches a high level of comprehension.