How to Make Bash Script Executable Using Chmod
Last updated
Last updated
In this tutorial, I am going through the steps to create a bash script and make the script executable using the command. After that, you will be able to run it without using the sh or bash commands.
The first step is to create a new text file with .sh
extension using the following command.
Open the newly created file using any of your favorite editors to add the following bash script to the file.
Save and close the file using ESC +:wq!
There are two ways to run the bash file. The first one is by using the bash command and the other is by setting the execute permission to bash file.
Let’s run the following command to execute the bash script using bash or sh command.
or
The second way to execute a bash script is by setting up the executable permissions.
To make a script executable for the owner of the file, use u+x filename.
To make the file executable for all users use +x filename or a+x filename.
After you have assigned the executable permissions to the script, you can run the script without bash command as shown.
In the following example, I am going to write and execute a bash script to take a backup from source to destination.
Save and close the file using :wq!
and give it the executable permissions for all users using the following command:
Now run the script:
At the end of this tutorial, you should be familiar with how to set a script executable in Linux.
I hope you enjoyed reading it, please leave your suggestion in the below command section.