Create Bash Script & File Permissions

Apr 21, 2022
4 min read
629 words

Let's Recap

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.

bash

touch first.sh

Note- Her .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.sh it will open command line editor and you can edit your bash file.

nano first.sh

  • 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-

first.sh

#! /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

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 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 As you can see in the above image we have successfully printed Hello World.

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

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.

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.

Jatin's Newsletter

I write monthly Tech, Web Development and chrome extension that will improve your productivity. Trust me, I won't spam you.

Share on Social Media: