# assigning a variable value to another variable It’s important to remember that when referencing a variable value you use the dollar sign, but when referencing the variable to assign a value to it, you do not use the dollar sign. Running the script produces the following output:Įach time the variable is referenced, it produces the value currently assigned to it. Just like system variables, user variables can be referenced using the dollar sign: Variables defined within the shell script maintain their values throughout the life of the shell script but are deleted when the shell script completes. The shell script automatically determines the data type used for the variable value. Here are a few examples of assigning values to user variables: No spaces can appear between the variable, the equal sign, and the value (another trouble spot for novices). Values are assigned to user variables using an equal sign. This little rule often gets novice script programmers in trouble. User variables are case sensitive, so the variable Var1 is different from the variable var1. User variables can be any text string of up to 20 letters, digits, or an underscore character. Setting variables allows you to temporarily store data and use it throughout the script, making the shell script more like a real computer program. A shell script allows us to set and use our own variables within the script.
The backslash allowed the shell script to interpret the dollar sign as an actual dollar sign, and not a variable. To display an actual dollar sign, you must precede it with a backslash character: In this example the script attempted to display the variable $1 (which was not defined), and then the number 5. Whenever the script sees a dollar sign within quotes, it assumes you’re referencing a variable. There is a drawback to using this method, however. Also notice that we were able to place the $USER system variable within the double quotation marks in the first string, and the shell script was still able to figure out what we meant. Notice that the environment variables in the echo commands are replaced by their current values when the script is run. # display user information from the system. This is demonstrated in the following script: We can tap into these environment variables from within your scripts by using the environment variable’s name preceded by a dollar sign.
To Print the value of above variables, use echo command as shown below : Some of the system defined variables are given below :
We can see these variables by using the command “ $ set“. Generally these variables are defined in CAPITAL LETTERS. These are the variables which are created and maintained by Operating System(Linux) itself.