A “sh” file in Linux is a script file by convention. It contains sequences of commands that we use for automation, or for running when the user isn’t around. In this article, I’ll explain how script files work, what you need to run them, and how to run them.
Running a “sh” File in Linux
Here’s a step-by-step guide to running a Linux script file.
1. Create the File
The first step is to create a file with a command and name it with a “.sh” extension like so:
For this example, I’ll just quickly create a script file with the command:
echo "Hello, World!"
This is the most basic script file I can think of.
2. Add the File Permissions
It’s not enough to create a script file. If you try and run it “as is”, you’ll get a “Permission Denied” message as shown here:
This is because the users don’t have execute permissions for the script. To add these permissions, we need to use the chmod command like this:
chmod +x script.sh
This modifies the script’s permissions as shown here:
You can see that when the file permissions change, the color code in the editor also changes. This is because Linux has recognized that this is a script file with the new permissions. The color green isn’t set in stone, and different SSH clients and environments can choose to set it to something else. The point is, that in many systems, you’ll get a visual cue for the script to distinguish it from ordinary files, so you know that it can be executed.
The new file permissions look like this:
-rwxrwxr-x
We have another article on the NameHero blog about file permissions, so I won’t explain what this means other than to say that the “write” permission represented by the “x” character is available to the owner, the group members, and all “others”.
3. Run the Script
To run the script, we simply need to type the name of the file with the fully qualified location. If the script file is located in the same directory in which you’re running it, you just write it like this:
./script.sh
This gives the output as follows:
And we get the output as expected.
As you can see, running a script “.sh” file is easy. But there are many more aspects to it as explained below.
“sh” Is Not a Required Extension
Even though the title of this article is about “.sh” files, and even though all the script files you’ve seen probably have the .sh extension, it’s not a formal standard in the Linux world that all script files should look like this. You can have a script file with any extension – or no extension at all, and it wouldn’t matter. The process of creating the right permissions and executing it would be the same.
However, from a standards point of view, there are very good reasons why everyone has coalesced around script files having the “.sh” extension. Here are some of them.
1. Easy to Identify
I showed you earlier that my environment displays all script files in green, and other tools might identify them in different colors. But there’s no guarantee that this will be true, and if you’re accessing your Linux system in an uncommon way, or through a file explorer via a GUI, there’s no quick way to check if a file is a script short of opening it to see its contents and examining the file permissions.
With all script files having the “.sh” extension as a matter of convention, it makes it easy to identify them regardless of the environment in which you view them. You can then sort the directory by filtering for script files to see which ones exist. None of this would be possible if we didn’t identify script files with a standardized extension like “.sh”.
2. Necessary for Tools and Integrated Editors
While it might be fun to play around with scripts, and it’s certainly easier to create scripts that run a few commands together for easy automation, the real heavy lifting happens when your script files are full-blown applications. Apart from running commands, script files in Linux also have control capabilities like “if-else” statements and more. So you can imagine that the length and complexity of these scripts can increase without bounds.
When you reach that stage, writing scripts on the command line won’t cut it, and not even a good command-line-based text editor is good enough. What you need is an “Integrated Development Environment”, or an IDE, with syntax highlighting, debugging, and integration with version control systems like git.
These IDEs assume that script files will have “.sh” extensions, and this allows them to highlight them in the code to make it easy to read. Using “.sh” lets them manipulate scripts, store them in separate folders, and more. If you want to take full advantage of IDEs for scripting, then using the “.sh” extension for scripts is crucial.
Best Practices for Running Scripts in Linux
It’s easy to start creating scripts, but before you get too far, here are some best practices for running “.sh” files that you’ll find useful – both for yourself and others.
Write Plenty of Comments
If you have experience with programming, you know how important comments are. While everything might seem clear to you at the moment, particularly if you’re the one who created the script file, take it from me that months down the line, you might not even recognize who wrote the script file in the first place! A comment lets you orient yourself and anyone else reading the script code and makes it easier to explain your decisions.
To write a comment, use the following syntax:
# Your comment here
Just prefix your comment with a hash (#), and comment away!
Use Functions to Encapsulate Code
It might be easier for small scripts to just put everything in a single code block. But as you become more proficient, you need to start using functions for complex operations. Functions let you reuse code for common operations and make your code easier to read.
Consider the following code:
# Define a function to greet a user
greet() {
local name=$1
echo "Hello, $name!"
}
# Example usage
greet "Alice"
Here, “greet” is a function that says “Hello xyz”, depending on what “xyz” is. You can reuse this function as many times as you want with different names, and when you change the function, it affects all the instances where it was called. This is what makes functions so powerful in Linux “.sh” scripts.
Conclusion
As you can see, getting started with Linux scripts and “.sh” file extensions is easy. Just ensure you’ve assigned the appropriate write permissions and are set. Scripts in Linux can make your task as an administrator much easier, since you can also schedule them to run at certain times via cron job, for example, and you don’t have to be physically present to execute them.
Just make sure that you sprinkle them liberally with comments and use functions whenever you can to encapsulate functionality and you’ll be a Linux scripting master in no time!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply