Variable Assignment in Bash
Entering a variable in the Bash script is simple, so let’s start with a simple example to print a variable:
echo "Name of the content write is $NAME"
Once we execute the script, it automatically prints the entered variable in the output:
Next, let’s create a script to give a clear overview of the variable assignment in Bash. For example, if we want to assign multiple variables, we can use the following script:
read var_1 var_2 var_3 var_4
echo "$var_1 is the best."
echo "However, you can try $var_2."
echo "Or else you may use $var_3."
echo "In case you are a beginner, $var_4 is the best."
You may get the following result by executing the script in the terminal:
Similarly, you can assign the numbers to print the script’s output accordingly. For instance, you can change the value of “X” and print them in the terminal through the following script:
echo "Value "X" is now $X."
echo "Value of "X" is now $X."
echo -n "values of "X" in the loop are:"
for X in 4 5 6 7
echo -n "$X"
Now, let’s execute the script, and the system will ask you to assign a variable to print the output:
This is how you can assign the variables in the Bash script. The read command is simple and prints the input in the terminal. Moreover, we also included a different example to explain the variable assignment in Bash. If you want to know more about the Bash-related stuff, don’t worry because Linuxhint has everything for you.