– I have downloaded a Linux script or tool from the internet which has “.sh” format, how do I run or install it?
– I am getting a “permission denied” error while trying to execute the “.sh” file.
I usually run into this question from users that have just begun exploring Linux or Mac as their secondary operating system. An “.sh” file is nothing but a “bash script”, also referred to as a “shell script”. These scripts can be run directly using the Linux terminal or any Unix like OS for that matter, which includes a Mac.
How to run an .sh file?
- Open the terminal of your system
- CD into the directory of the script
i.e. (cd /home/nishant/Downloads/)
- Ensure that the script has executable permissions but running the following command.
chmod +x your-file-name.sh
- Now you can run the script by by using any of the following commands.
How to run a bash script as root?
For any script that makes changes to your operating system as a whole would need to be as a root user. A few quick examples of scripts requiring sudo access would be
- Installing a software
- Removing an application
- Making changes to the files outside of your user account
Assuming your account does have sudo access provisioned when it was created, you can make use of the following approaches to run your .sh file as a root user on Linux / Mac / Unix system.
sudo bash your-file-name.sh
Alternatively, you can become root and then execute the script directly.
This will elevate your privileges to “root” and keep you in the same directory.
How do I change the owner of .sh file?
Sometimes you might run into “permission denied” error if the .sh file is not actually owned by you. In that case you would want to ensure that you change the ownership of the file to your user account as its advised to avoid using root privileges unless necessary. You can use the following command to change ownership of a file. Your account would need sudo access in order to execute this account.
Changing ownership of an .sh file
sudo chown user:group your-file-name.sh
Changing ownership of a directory with .sh files
sudo chown -R user:group your-file-name.sh
In most cases your “user” and “group” would be the username of your Linux account. And “-r” argument in the case of directory ownership change represents “recursive” flag, so all the files inside the directory would be owned by the newly defined user.
That is every you need to know about executing a bash script or an .sh file on a Linux or a Mac computer. Feel free to leave a comment below if you need further clarification.