Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • How to Make Bash Script Executable Using Chmod
  • Step 1: Creating a Bash File
  • Step 2: Writing a Sample Script
  • Step 3: Executing the Bash Script
  • Step 4: Set Executable Permissions to Script
  • Step 5: Running Executable Script

How to Make Bash Script Executable Using Chmod

Previousscripting listNextShell Script to Check Linux Server Health

Last updated 1 year ago

How to Make Bash Script Executable Using Chmod

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.

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 ESC +: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 for the owner of the file, use u+x filename.

chmod u+x hello_script.sh

To make the file executable for all users use +x filename or a+x filename.

chmod +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/ubuntu/backup-$BACKUPTIME.tar.gz
SOURCE=/data_folder
tar -cpzf $DESTINATION $SOURCE

Save and close the file using :wq! and give it the executable permissions for all users 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 it, please leave your suggestion in the below command section.

chmod
bash example script and execute
Execute the bash script
Running a executable script