Table of Contents
Create Bash Script & File Permissions
In the previous article 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.
Let's Create Bash 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.
.sh is an extension that you have to provide for execution.
Step-2: Edit the Script
- To edit the content of this file you can run
nano first.shit will open command line editor and you can edit your bash file.
- Now we need to add some stuff in this file first put
#! /bin/bashin 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 Worldin the file in next line so your bash script will look like-
#! /bin/bash echo Hello World
- After you are done then just save the file, to save the file in windows follow these steps-
Ctrl + Xfirst.
- Then press
- Then press
- Now your changes have been saved.
Step-3: Run the Script
To do the type the following in the command line-
After runnnig this your screen will look like this- 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-
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
As you can see in the above image we have successfully printed
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 (
- Write - Permission to modify the file content. represented as (
- Execute - Permission to run the file or script. represented as (
You can change the permission of the file by using
chmod command. Syntax of
chmod is -
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
-) 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
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.
I write monthly Tech, Web Development and chrome extension that will improve your productivity. Trust me, I won't spam you.