How do you declare and assign variables in Linux?
In Linux, variables can be declared and assigned using the following two methods:
- Assigning values using the equal sign:
Variables can be referenced by adding the dollar sign ($) before the variable name and assigning a value to the variable using the equal sign (=). For example: - John is the name and he is 25 years old.
- In the example above, the variable name is assigned the value “John” and the variable age is assigned the value 25.
- Assigning values using the read command:
You can use the read command to receive variable values from user input during runtime. For example: - Enter your name and it will be displayed back to you.
- In the above example, the read command is used to receive input from the user and assign it to the variable called name, then the echo command is used to display the value of the variable.
You can reference the value of a variable using the dollar sign ($) regardless of which method is used for assignment. For example, you can use the echo command to print the value of a variable.
echo $name
echo $age
The above command will print the values of the variables name and age.