How to run the .sh file in Linux
A .sh file in Linux is an executable shell script containing a series of commands that will run sequentially. It’s commonly used to streamline complex operations, automate tasks, and run programs.
Understanding how to create and run a shell file is crucial when you’re managing a Linux system. It helps make the administration process more efficient and less prone to errors.
In this tutorial, you’ll learn how to run a .sh file in Linux using the command line and the graphical interface. We’ll also explain how to create this file and set the correct permissions for seamless execution.
What is a .sh file?
A .sh or shell script file is an executable item containing a series of commands. When you run it, your shell or command-line interpreter will read its content and iterate through the commands.
This file is commonly used in system administration because of its benefits, such as:
- Efficiency – instead of running multiple commands individually, you can complete a complex task by executing a single shell script file.
- Consistency – a .sh file always performs the same task and gives the same output, minimizing human errors.
- Automation – using cron job, you can automatically run a .sh file at a specific interval or a particular time to streamline repetitive tasks.
- Reusability – you can easily share a .sh file with other team members or reuse it in another project to perform the same task.
- Maintainability – .sh files are easily edited, tested, and debugged because they are similar to other text files.
How to create a .sh file?
The steps to create a .sh file are the same regardless of your chosen Linux operating system. However, they differ depending on whether you use a command line or a graphical user interface (GUI).
Creating a .sh file with commands
Before creating a .sh file, open your system’s terminal to access its command-line interface (CLI). If you’re working with a remote Linux server, connect to it via SSH using PuTTY or terminal.
Alternatively, Hostinger Linux VPS hosting users can easily connect to their server directly via their web browser using hPanel’s Browser terminal.
Once connected, follow these steps to create a new shell script file:
- Run the following command to create a blank .sh file. Replace script with your desired name:
sudo touch script.sh
- Open the new file with your preferred text editor. In this tutorial, we will use Nano:
sudo nano script.sh
- The text editor will open. Write the following shebang line at the top to indicate which shell interpreter your system should use to read the file’s content. For example, we will make a bash script:
#!/bin/bash
- In the next line, add a few commands in chronological order. It might look like this:
# Update the repo sudo apt update # Install available updates sudo apt upgrade
- Once finished, hit Ctrl + X, Y, then Enter to save the file.
Important! Your system shell interpreter will ignore lines that start with a hash mark (#), except the shebang. This is commonly used to add comments or notes, providing anyone reading or editing the script with more context.
Creating a .sh file via GUI
Here’s how to create a .sh file via GUI on your Linux desktop. The general procedure should be the same regardless of your distribution, but the button location might differ slightly.
In this tutorial, we’ll show you how to do it in Ubuntu:
- Click Show Applications from your desktop’s launcher.
- Select Text Editor.
- Write the shebang and your commands. Hit Save.
- Select the location where you want to save the script. Click Save to confirm.
- Enter a name for your script. Make sure it ends with the .sh extension.
- Hit Save.
Setting permissions to run a .sh file
For security reasons, you can’t execute .sh files in Linux by default. You must set the appropriate permissions before running them.
To make the file executable, add the corresponding permission using the chmod command as below. Remember to replace the file name accordingly:
sudo chmod +x script.sh
Note that this command will only grant executable permission to the file owner or creator. This means another user can’t run the script as a program unless using sudo.
Important! While you can add executable permission for all users, we don′t recommend it for security reasons. The best practice is to only make a bash script executable for the owner.
You can also change your .sh file permission via the GUI. For instance, here’s how to do it in Ubuntu:
- Right-click your .sh file and select Properties.
- Select the Permissions tab.
- Check the Allow executing file as program box.
- Close the dialog window.
Although you can execute a .sh file, you might be unable to run all its content. When the script runs, a permission error might still arise.
This happens because the current user doesn’t have enough permissions to run commands or modify an item specified in the .sh file.
Running a .sh file
There are various ways to run a .sh file for specific use cases. In this section, we’ll cover four of the most common ones.
Running a .sh file normally
The most basic way to run a .sh file is by entering its name in your command-line interface like so:
./script.sh
This command only works if your .sh file is located in the current directory. If your script is located in another folder, specify the full path like so:
./path/to/folder/script.sh
If you use a GUI, you can easily run a .sh file by right-clicking it and selecting Run as a Program.
While simple, this method’s downside is that your script can only run using the shell interpreter specified in the shebang.
Using a specific interpreter
In some cases, you may need to run your shell script using a specific interpreter. This is useful in several scenarios, like when you don’t have permission to execute the file.
Running a non-executable .sh file
You can technically run a non-executable script by explicitly specifying the shell interpreter. However, this won’t execute the file as a program – it will only read the file’s content.
This doesn’t present security risks since the user still needs to have permission to execute commands and modify files specified in the script.
You can run your .sh file using a specific shell by specifying it in the beginning of your command. This will overwrite the interpreter specified in the shebang.
There are various other shell interpreters with different behaviors. The two most common ones in Linux are sh and its modern version, bash. Here’s what your commands will look like when running a script using either of those shells:
bash script.sh sh script.sh
Running a .sh file as another user
System administrators run .sh files as a specific user or group for a few reasons. For example, dedicating a particular account to execute scripts for many similar operations helps simplify task management.
For security reasons, it’s also advisable to run a .sh file as the owner instead of root. This procedure prevents you from granting excessive privileges during the operation, which can damage another part of your system.
For example: you run a script that cleans up a directory, but the variable specifying the path is missing. If you run this .sh file as root, you’ll delete the main /home directory. This won’t happen with non-root users because their permissions are insufficient.
To run a .sh file as another user, use the following command syntax:
runuser -u [username] -- [/path/to/script.sh]
For example, if you want to run a monitoring script as the cron user, your command will look like this:
runuser -u cron --/program/automation/monitoring.sh
If you wish to run a .sh file as a specific group, simply add the -g option to the above command.
runuser -u cron -g automation --/program/automation/monitoring.sh
In the example above, we are running the monitoring.sh script as the cron user within the automation group.
Executing a script in the background
By default, .sh files will run on your main terminal shell. This can be problematic if you’re executing a script that needs to run constantly, like for monitoring or logging.
In this case, run your .sh file in the background so you can still use the main terminal shell while the script is running. The easiest way to do this is by appending an ampersand (&) at the end of your command:
./script.sh &
Terminal will output the script’s process ID, which you can use later to stop the process using the kill command.
If you wish to keep the script running in the background after closing terminal, you can use the nohup command:
nohup ./script.sh &
A more organized way to run a script in the background is using a terminal multiplexer like GNU Screen or tmux. This tool creates a virtual shell that can continue running even after you disconnect from the SSH session.
Conclusion
A .sh file is an executable script containing a series of commands. It is mainly used to streamline complex operations, run programs, and automate tasks in Linux.
To create a .sh file, simply use the touch command, then add commands to the file using a text editor like Nano. If you’re using a GUI, just open a text editor to write and save your script to a chosen location.
Then, make the .sh file executable by adding the appropriate permission using the chmod command. You can do it in the GUI by going to the Permission tab in the file’s Properties menu.
Run the .sh script by specifying its name in the Linux terminal. Alternatively, you can explicitly pass it to a shell interpreter like bash. For GUI users, right-click the file and click Run as a Program.
.sh file FAQ
What is a .sh file?
A .sh file in Linux is an executable item containing a series of commands. Think of it as a script that defines a sequence of actions your system’s command line should complete.
This file is useful for automating tasks, running programs, or streamlining command execution. In addition to saving time and effort, it helps reduce human error.
What permissions are needed for a .sh file?
A .sh file requires all permissions to run properly, which are read, write, and execute. These ensure you can view, edit, and run the script.
However, you typically only grant these privileges to the file owner for security reasons. For other users, they can run the file using sudo.
Can I edit a .sh file with any text editor?
Yes. A .sh bash script works like any other file extension, meaning you can edit it using different text editors. For example, you can open it using Nano, Vim, Gedit, and others.
However, make sure you have write permission enabled on the .sh file, because you won’t be able to edit it otherwise.