A Step-by-Step Guide to Creating a Bash Script in Linux

Anurag
3 min readApr 14, 2023

--

Introduction: Shell scripting is a powerful tool that allows users to automate tasks, manage files, and interact with the operating system. The Bash shell is a widely used shell in the Linux and Unix environments and is known for its power and flexibility. This article provides a step-by-step guide to creating a Bash script.

Step 1: Types of Shells Before diving into creating a Bash script, it’s essential to understand the different types of shells available in the Linux environment. Shells are responsible for interpreting and executing commands entered by the user or a script. The /etc/shells file contains a list of valid shells in the system. In the example script, the command cat /etc/shells is used to display the list of shells available.

Step 2: Location of Bash The location of the Bash shell can be found using the which bash command. The output of this command is the file path to the Bash shell.

Step 3: Mentioning Bash File Path The Bash file path is mentioned at the beginning of a script using the shebang notation #! /bin/bash. This tells the operating system which shell to use for executing the script.

Step 4: Creating a Bash File To create a Bash script, a file with a .sh extension must be created. In the example script, the command touch hello.sh creates a new file called hello.sh.

Step 5: Changing File Permission To execute a Bash script, its permissions must be set to executable. This can be done using the chmod +x hello.sh command.

Step 6: Running the File To run the Bash script, the command ./hello.sh is used. The ./ notation tells the shell to execute the script in the current directory.

Step 7: Variables Variables are used to store and manipulate data in a Bash script. The example script shows that variables can be of two types — system variables (in Caps) and user-defined variables (generally in lowercase but can also be in Caps). There is no strict rule for naming user-defined variables.

Step 8: Naming Variables It’s essential to note that variable names should not start with numbers.

Reading User Input: Bash scripts can read input from the user using the read command. In the example script, different variations of the read command are used to read input from the user. For example, the -p option is used to display a prompt, while the -sp option hides the input on the screen.

Conclusion: In summary, this article provided a step-by-step guide to creating a Bash script. It covered the different types of shells available in the Linux environment, the location of the Bash shell, how to create a Bash file, set file permissions, and run the script. Additionally, it introduced the concept of variables and how to read input from the user using the read command. By following these steps, users can create powerful scripts that automate tasks and interact with the operating system.

--

--

Anurag

Currently working as Product Manager who is also a passionate engineer with an experience in Artificial Intelligence.