Understanding Comments & Variables in Bash Scripting
Understanding Comments & Variables in Bash Scripting
We will be looking at comments, variables, & arguments in Bash Scripting
Table of contents:-
- Comments
- Variables
- Arguments
Requirements:-
- A Linux machine if you want to follow along with me( I am using a kali-Linux docker container ), if you want to learn docker you can visit Docker Tutorial
- You need to know how to run a bash script of bash basics ( you can visit Bash Scripting Basics )
What are comments & how to use them?
Comments are nothing but the lines of code, which the computer ignores while running the program.
In bash scripting to write a comment, we have to use # where we want to use the comment.
You can also say that comments are for the understanding of the programmer.
As you can see above we have executed the file but nothing happened if you see inside the shellscript.the sh file you will see a # before an echo command, it's the way to use the comments.
What are variables?
In computer programming, a variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.
In simple words, we can say that a variable is a container that stores some value.
There are 2 types of variables.
- System Variables - They are most likely in the capital forms & managed by the Operating System ( e.g - $PWD, $SHELL, etc )
- Users define Variables - These are the variables defined by the user.
Syntax -
1. variable_name = variable_value
2. Name = "Lets Configure"
Taking input from the user
In the above example, we have seen the fixed value variables, now you want sometimes the user to enter something and you do tasks according to it.
To take the input from the user read command is used.
As you can see above the read command is taking the input from the user & stores it in the variable name... ( Note:- As you can see while executing the script it asked me for my input but on the next line if I want to give in the input on the same line you have to use an option given below )
As you can see above, we have used a option -p which allows us to give what user will see before asking for the input & where to store it on the same line ( And you might have noticed when we executed the bash script it asked us for the input on the same line or same prompt unlike the simple one we have seen above )
There are so many arguments you can pass to the read command you can see the help page for that to find out more. ( read --help )
When you don't give any variable name to the read-option it automatically goes to the $REPLY variable which is the system variable
When you want to give multiple inputs to the user then you can save the inputs given by the user into an array, In Simple words, an array is nothing but the list of data for now. ( You have to use the option -a for that )
As you can see above we have used an option -a after the read command & named the variable, then we have echo the names with the curly brackets, the numbers in the programming language start with 0 which is called an index. We will look into that later
We will look at more stuff about the bash in the later part