DEV Community

Cover image for Create Bash Script & File Permissions
Jatin Sharma
Jatin Sharma

Posted on • Updated on

Create Bash Script & File Permissions

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.

Table of Contents

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.
touch first.sh
Enter fullscreen mode Exit fullscreen mode

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-
#! /bin/bash
echo Hello World
Enter fullscreen mode Exit fullscreen mode
  • 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-

./<fileName>.sh
Enter fullscreen mode Exit fullscreen mode

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-

chmod +x first.sh
Enter fullscreen mode Exit fullscreen mode

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 -

chmod [class][operator][permission] file_name
Enter fullscreen mode Exit fullscreen mode
  • 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. 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

You might be interested in

Top comments (0)