> For the complete documentation index, see [llms.txt](https://morgan-bin-bash.gitbook.io/bash-scripting/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://morgan-bin-bash.gitbook.io/bash-scripting/how-to-make-bash-script-executable-using-chmod.md).

# How to Make Bash Script Executable Using Chmod

## 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 [chmod](https://linuxopsys.com/topics/chmod-command-in-linux) 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
```

<figure><img src="https://linuxopsys.com/wp-content/uploads/2022/08/execute-bash-script.png" alt="Execute the bash script" height="110" width="700"><figcaption></figcaption></figure>

### 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
```

<figure><img src="https://linuxopsys.com/wp-content/uploads/2022/08/running-executable-script.png" alt="Running a executable script" height="195" width="700"><figcaption></figcaption></figure>

#### 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
```

<figure><img src="https://linuxopsys.com/wp-content/uploads/2022/08/bash-example-script-execute.png" alt="bash example script and execute" height="200" width="705"><figcaption></figcaption></figure>

#### 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://morgan-bin-bash.gitbook.io/bash-scripting/how-to-make-bash-script-executable-using-chmod.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
