FUNDAMENTALS A Complete Guide for Beginners
Bash scripting is a powerful tool that allows you to automate tasks, streamline processes, and enhance your productivity as a developer or system administrator. Understanding the fundamentals of writing Bash scripts is essential for anyone seeking to harness the full potential of the command line interface. This article will explore the basics of writing Bash scripts, covering important concepts such as the Bash symbol, shebang, script commands, and chaining multiple commands.
Key Takeaways
- Getting familiar with the process of writing Bash script.
- Getting familiar with the utilities of Bash script.
Download File
Step-by-Step Guide to Write a Bash Script
While programmers write Bash script, they follow some steps sequentially. Here, I have discussed these steps briefly.
1. Getting Started: Initiating a Bash Script
To begin writing a Bash script, open a text editor and create a new file with the “.sh” extension. This extension is commonly used to indicate that the file contains a Bash script. At the start of your script, it is important to include the “shebang” line.
2. The Shebang Line: #!/bin/bash
The shebang line, also known as the shebang or the interpreter directive, informs the system which interpreter should be used to execute the script. In the case of the Bash script, the shebang line is “#!/bin/bash”. This line should always be the first line of your script.
3. Bash Script Commands
Bash script allows you to execute commands and perform various operations. Examples of commonly used Bash Script commands are echo, read, if, else, and elif. There are some special symbols for chaining commands which help the programmer execute multiple commands in a single line.
Some Practical Examples of Writing a Bash Script
In this section, I have demonstrated some sample Bash scripts that will help you to understand the basic concept of a Bash script.
Example 01: Printing a Greetings on the Terminal
Here I will develop a Bash script that will print a greeting on the terminal. To do so, follow the below procedures.
❶ At first, launch an Ubuntu Terminal.
❷ Write the following command to open a file in Nano:
nano sample.sh
- nano: Opens the nano text editor.
- sample.sh: Bash script name.
❸ Copy the script mentioned below:
#!/bin/bash
echo"Happy Scripting" #prints the greetings on the terminal
The #!/bin/bash interprets that it is a Bash script. Then the echo command prints greetings on the terminal.
❹ Press CTRL+O and ENTER to save the file; CTRL+X to exit.
❻ Run the script by the following command:
bash sample.sh
- bash: Executes the bash scripts.
- sample.sh: Bash script name.
The above image shows that Bash script has printed a greeting text on the terminal.
Example 02: Taking Input From the User Then Print It
You can take input from the user while running a Bash script and then print it on the terminal. Here I have developed a Bash script that takes input from the user and then prints it on the terminal. To do this task, follow the below script.
You can follow the steps of example 01, to save & make the script executable.
Script (bash.sh) >
#!/bin/bash
#takeing input from users
read -p "What is your name?: " name
#printing on the terminal
echo "Welcome $name!"
At first, the Bash script asks for input from the user and keeps it on the name variable. Then the echo command prints a line on the terminal which contain the value of name variable.
Execute the following command into the terminal to make the Bash script executable.
chmod +x bash.sh
- chmod: Change the permission of a file.
- +x: Gives execution permission to all.
- bash.sh: Bash script name.
Run the script by the following command:
./bash.sh
In the above image, you can see that the Bash script has taken input from the user and then printed it on the terminal.
Example 03: Performing Arithmetic Operations by Taking Two Input Numbers from the User
Programmers can do arithmetic operations on the Bash script. Here, I have developed a Bash script that takes two inputs from the user and then does an arithmetic summation. Afterward, print the summation on the terminal. To do so, follow the below script.
You can follow the steps of example 01, to save & make the script executable.
Script (arith.sh) >
#!/bin/bash
#taking number as input from user
read -p "Enter First Number: " a
read -p "Enter Second Number: " b
#calculation summation
var=$((a+b))
#printing the summation value
echo "Summation of The Two Number:$var"
At first, the Bash script has taken numbers a and b as input. Then I did the arithmetic summation. And finally, print the summation on the terminal.
Run the script by the following command:
./arith.sh
The above image shows that the Bash script has taken input from the user then done an arithmetic summation.
Example 04: Using a Function in the Bash Script
You can easily use a function in the Bash script. Here, I have developed a Bash script where the function is used to print a message. To do so, follow the below script.
You can follow the steps of example 01, to save & make the script executable.
Script (bash_fun.sh) >
#!/bin/bash
# A sample bash function
sample_function () {
echo "Good Morning!"
}
# Invoking the sample_function
sample_function
At first, a function named sample_function has been defined. In side the function the echo command prints a message. In the next part of the Bash script, sample_function has been called by simply putting the function name.
Run the script by the following command:
./bash_fun.sh
The above image shows that the Bash script has printed a message using a function.
Conclusion
Writing Bash script empowers you to automate repetitive tasks, manage complex operations, and boost your efficiency as a developer or system administrator. By understanding the fundamentals of Bash scripting, including the Bash symbol, shebang, script commands, and chaining multiple commands, you can create powerful scripts to streamline your workflow. Embrace the power of Bash scripting, and unlock new possibilities in the command line realm.
People Also Ask
Related Articles
- How Will You Start Writing Bash Scripts [A Complete Overview]
- An Ultimate Guide to Bin Bash Script and Shebang
- Most Used Bash Script Commands [The Ultimate Review]
- 16 Most Used Symbols for Bash Script
- 8 Practical Examples of Chaining Commands in Bash Scripts
<< Go Back to Bash Scripting Basics | Bash Scripting Tutorial