Updated April 15, 2023
Introduction to Bash Variable in String
Variable is one of the most widely used, or in other words, we say it to be the most essential feature of programming where it provides an ability to the programmer to reference a name or a label to some other quantity. As the name suggests, the variable is something that can vary. For example, in our mathematics, we assume x as 10. Here it is important to know that during manipulation of the equation in mathematics the value which x is storing might change, but the value 10 will always be value 10. Similarly, in computers, we use a variable so that it can be assigned some value and can either be referenced to print or access for manipulation. In this topic, we are going to learn about Bash Variable in String.
In the programming world, the variable is thought to be an advanced programming concept, where the programmer would use variable only when the value is not known to the code from the start. For example, if we write a program to calculate the sum of 10 & 20. We know that the values can only be 10 and 20 and nothing else! We would never use variables. But in case, we don’t know what to add, we would take the help of the concept of variables, and give the flexibility to the user to enter the numbers to be added. We assume the variable to be glass which will be used when we fill water to it, or in programming words used only when the program is executed!
Now the next question we would like to answer is, how do we set a variable or in other words what’s the syntax for setting up a variable. One picky thing about bash is its “allergy” to whitespaces. In most of the programming languages, whitespaces are “cool” to use but not for the bash.
What is correct?
Var_1=“EduCBA”
What is not correct?
Var_1 = “EduCBA”
Var_1= “EduCBA”
Var_1 =“EduCBA”
Now, when we have allotted a value to a variable, how do we reference it? For this, a special character is employed and that’s $. Any variable that follows a dollar sign is attempted to be replaced with the value of the variable stores. This technique is known as variable expansion. In case it is not able to reference the variable it will throw out an error. We will discuss a small deviation in the next section, but first, let us look at what’s correct and what’s wrong?
What is correct?
Var_1=“EduCBA”
echo $Var_1
echo “$Var_1”
What is not correct?
Var_1 = “EduCBA”
echo Var_1 [This will just print the variable name i.e. Var_1]
echo $Var_2 [Var_2 is not declared initially, would throw an error]
Failure to reference will occur in 2 cases, but not necessarily throw an error:
- If the dollar sign doesn’t precede the variable name (Shown in the example, we discussed above)
- If the dollar preceding the variable name is enclosed within single quotes. For example, echo ‘Var_1’. In this case, it will just print out the variable name (Var_1) and not its value
Now coming to some important features of string:
Concatenating Strings
In case one needs to store a string, which is likely to be copy-pasted with every other value, one can store it in a variable and concatenate it with the values. For example, if one needs to add https://www. before every website name and end it with .com can take the help of the variable and concatenate it to the value of the website name.
Var_start="https.//www."
Var_end=".com"
echo $Var_start$1$Var_end
The echo statement will print out the value stored in Var_start followed by the input entered by the user ($1) and finally end with the value stored in Var_end.
Allowed Variable Names
All variable names can contain the sequence of alphanumeric characters, but the ones created by the user should start only with either alphabets or an underscore.
What is correct?
Var_1, _Var1, Var1
What is not correct?
1Var, #Var, Var 1, etc.
Command Substitution
Bash allows standard output to be encapsulated and then expanded when executed in the shell. For example, the seq command will print the sequence of numbers given as an argument.
Arithmetic expansion
For bash one needs to perform calculations inside $(( )). In cases of advanced mathematical utilities, we use the command bc which helps in evaluating an expression. We would see some utility of the same in our examples.
Example of Bash Variable in String
Now its time for us to look into the above theoretical concepts in the real-time using example.
In the code below we will try to cover all the utilities we talked about above in a single code, obviously separated by some headings.
Code:
echo "***Assigning a variable***"
Var_1="EduCBA"
echo "***Printing Var_1 which is assigned value of EduCBA***"
echo $Var_1
echo "***Printing Var_2 which is not assigned***"
echo "***Nothing gets printed!***"
Var_start="https://www."
Var_end=".com"
echo $Var_start$1$Var_end
echo "***Sequence keyword will print 1 to 4 in different lines***"
seq 1 4
echo "***The following command will print it in single line***"
echo $(seq 1 4)
echo "***Arthmetical operator to add 2 numbers entered by users: $2 and $3"
echo $(($2+$3))
echo "***Using bc utility to restrict the number of decimals***"
echo "7.5 / 2.1" | bc
echo "***Using bc utility to not restrict the number of decimals***"
echo "7.5 / 2.1" | bc -l
Output:
Conclusion – Bash Variable in String
In an ending note, we would encourage you to explore more on the hidden treasure of strings in bash. The capability and flexibility of bash variables in strings provide enables us to achieve any complex program within bash itself. Even using some sed and awk commands and usage of variables, one can easily achieve complex computation of data analytics as well!
Recommended Articles
We hope that this EDUCBA information on “Bash Variable in String” was beneficial to you. You can view EDUCBA’s recommended articles for more information.