30 September 2020
1 min read

How to Make Bash Script Executable Using Chmod

In this tutorial, I am going through the steps to create a bash script and to make the script executable using the chmod command. After that, you will be able to run it without using the sh or bash commands.

Step 1: Creating a Bash File

The first step is to create a new text file with .sh extension using the following command.

$ touch hello_script.sh

Step 2: Writing a Sample Script

Open the newly created file using any of your favorite editors to add the following bash script to the file.

$ vim hello_script.sh #!/bin/bash echo "Hello World"

Save and close the file using :wq!.

Step 3: Executing the Bash Script

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.

$ bash hello_script.sh

or

$ sh hello_script.sh

Step 4: Set Executable Permissions to Script

The second way to execute a bash script is by setting up the executable permissions.

To make a script executable use +x or u+x, for example :

$ chmod u+x hello_script.sh

Step 5: Running Executable Script

After you have assigned the executable permissions to the script, you can run the script without bash command as shown.

$ ./hello_script.sh

Another Example

In the following example, I am going to write and execute a bash script to take a backup from source to destination.

$ vim backup_script.sh #!/bin/bash TIME=`date +%b-%d-%y` DESTINATION=/home/kashif/backup-$BACKUPTIME.tar.gz SOURCE=/data_folder tar -cpzf $DESTINATION $SOURCE

Save and close the file using :wq! and give it the executable permissions using the following command:

$ chmod +x backup_script.sh

Now run the script:

$ ./backup_script

Conclusion

At the end of this tutorial, you should be familiar with how to set a script executable in Linux. I hope you enjoyed reading, please leave your suggestion in the below command section.

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is the editor-in-chief of Linoxide and has an experienced team of Linux enthusiastic authors who makes this blog awesome. Linoxide is one of the top 20 Linux Blog by whizlabs.

Leave a Reply

Your email address will not be published.

Previous Story

How to Upgrade Linux Kernel on CentOS 7

Next Story

2 Ways to Install Notepad++ on Ubuntu

Latest from Blog

Top 8 Reasons to Use Garuda Linux

Have you been going back and forth between multiple Linux flavors in search of an exciting experience? Or perhaps you are coming from a Windows or MAC environment and want to try

How to Rename Multiple Files in Linux

In a Linux system, you can easily rename a file using mv command. But, if you have multiple files which you want to rename, in this situation you need some extra tools

How to Install TensorFlow on Ubuntu 20.04

Tensorflow is an open-source platform for machine learning and artificial intelligence. It is developed by the Google Brain team. It contains tools, libraries, and community resources for developers to build ML powered
Go toTop