How to run a .sh file and Shell Scripts in Linux?

running .sh files in linux

If you have used Linux for some time, you probably have come across files with the .sh extension. Whether it might be during downloading a particular software or writing your own bash scrips, these files can be sometimes tricky since click and run doesn’t work with them. So let’s take a look at what these files are and how to do you run them.

Steps to run .sh file

A .sh file is a shell file. Shell files are scripts containing a list of commands that are supposed to be run by your Unix shells (bash, fish, zsh etc). You can write shell scripts to automate some aspects of your systems, do batch jobs, modify the system functionality, etc.

Step 1: Make the file executable

Before running a shell script you first have to give permissions to the file which will make it executable for you. We use the chmod command to make the file executable.

If you want to make the file executable for every user on the system, run the chmod command as below:

chmod a+x /path/to/sh/file 

If you want to make the file executable only for the owner of the file, run :

chmod u+x /path/to/file

Step 2 : Run the file

You can run the file after you have made it executable. To do so, run the following:

./path/to/sh/file

Here we have a simple script, example.sh which outputs a sentence and then the name of the current user.

How To Run Bash Script Example

Conclusion

Now you can run any shell script you write or download. But beware of running any script you find on the internet. If you don’t understand it, don’t run it should be the basic rule. To learn basic shell scripting you can use this interactive website to do so. Have fun scripting!