Create Bash Script & File Permissions
Tags: beginners, tutorial, programming, computerscience
Published: 2022-04-22 04:56:59

### Let's Recap
In the previous [article](https://dev.to/j471n/getting-stated-with-bash-29m3) we have learned what Bash is, How to install bash and how it functions on the command line. In this article, we are going to learn the bash scripting, and how we can create a bash script and how we can run it on our system.

### Table of Contents
- [Let's Create Bash Script](#lets-create-bash-script)
  - [Step - 1 : Create the Script](#step---1--create-the-script)
  - [Step - 2 : Edit the Script](#step---2--edit-the-script)
  - [Step - 3 : Run the Script](#step---3--run-the-script)
- [File Permissions](#file-permissions)
  - [Changing Permissions](#changing-permissions)
- [Wrapping Up](#wrapping-up)
- [You might be interested in](#you-might-be-interested-in)


<a id="lets-create-bash-script" />

## Let's Create Bash Script

<a id="step---1--create-the-script" />

### Step - 1 : Create the Script
-   To create an empty bash script, first, change the directory in which you want to save your script using  **cd**  command. 
-    Use  **touch**  command to create a bash script as shown below.
```bash
touch first.sh
```
**Note-** Her `.sh` is an extension that you have to provide for execution.

<a id="step---2--edit-the-script" />

### Step - 2 : Edit the Script
- To edit the content of this file you can run `nano first.sh` it will open command line editor and you can edit your bash file. 

![nano first.sh](https://i.imgur.com/bLIZ2se.gif)

- Now we need to add some stuff in this file first put `#! /bin/bash` in the top of the file.
	- `#!` - is referred to as the **_shebang_** and re st of the line is the path to the interpreter specifying the location of bash shell in our operating system.
- Now we add `echo Hello World` in the file in next line so your bash script will look like-
```bash
#! /bin/bash
echo Hello World
```
- After you are done then just save the file, to save the file in windows follow these steps-
	- Press `Ctrl + X` first.
	- Then press `Y` 
	- Then  press `Enter`
	- Now your changes have been saved.
	![save a file](https://i.imgur.com/99B5757.gif)

<a id="step---3--run-the-script" />

### Step - 3 : Run the Script
To do the type the following in the command line-
```bash
./<fileName>.sh
```
After runnnig this your screen will look like this-
![permission](https://i.imgur.com/6zQdAwV.gif)
It shows *permission denied*. It is because user don't have permission to execute the file right now. For that run the following command in the terminal-
```bash
chmod +x first.sh
```
**Note-** We will talk about the permissions in a second. For now it just changes the execution permission to true. `+` stands to add the permission and `x` stands for running or execution permission.

Now after authourizing execution run the previous command again which was `./<fileName>.sh` in our case it is `./first.sh`.

![runfirst.sh](https://i.imgur.com/GuZTZOU.png)
As you can see in the above image we have successfully printed `Hello World`.

<a id="file-permissions" />

## File Permissions 
Linux-based Operating System requires file permissions to secure its filesystem. There are three types of permissions associated with the files as follows-
- **Read -**  Permission to view the content of the file. represented as (`r`). 
- **Write -**  Permission to modify the file content. represented as (`w`). 
- **Execute -**  Permission to run the file or script. represented as (`x`).

<a id="changing-permissions" />

### Changing Permissions 
You can change the permission of the file by using `chmod` command. Syntax of `chmod` is -
```bash
chmod [class][operator][permission] file_name
```
- **Class** is represented by the indicators - **u, g, o,** and **a**, where-
	- `u = user`
	- `g = group`
	- `o = other` 
	- `a = all the classes`

- **Operator** ( `+` or `-` ) is used to add or remove the permission.
- **Permissions** have three types  as follows-
	 - `r = reading the script`
	 - `w = modifying the script` 
	 - `x = running the script`

Now I guess everything makes sense when we give execution permission (`+x`) to the `first.sh` script.

<a id="wrapping-up" />

### Wrapping Up
Firstly, we learn how to create the bash script, how to edit, and how to run the script. Also, we learned about the different file permission in the Bash and how to modify them. If you enjoyed this article or learn something new, then don’t forget to press ❤️. and for Future articles *Follow* me. If you have any queries or suggestions, don’t hesitate to drop them. See you.
> You can extend your support by buying me a Coffee.😊👇
[![buymecoffee](https://i.imgur.com/A6l6yWM.png)](https://bit.ly/3HXbKNC)

<a id="you-might-be-interested-in" />

### You might be interested in
-  [Getting Started with Bash ](https://dev.to/j471n/getting-stated-with-bash-29m3)