Variable
In simple terms, a variable is a character to which a value is assigned. Do you remember back when your algebra professor taught you that if x=2 and y=3, then x+y=5?
The same principle applies in Linux; the only difference is that generally, the variables used help us automate a task and or set a configuration. Variables are usually created in higher case letters but are not required to be.
Setting a Variable
[user@server ~]$ MY_PROGRAM_PATH=/home/user/public_html/myprogram
[user@server ~]$ MY_PROGRAM_KEY=/home/user/public_html/myprogram/.key
Now that we assigned values to variables, let us see if they took. To do so, we use the 'echo' command and prepend the character '$' before the variable.
[user@server ~]$ echo $MY_PROGRAM_PATH
/home/user/public_html/myprogram
[user@server ~]$ echo $MY_PROGRAM_KEY
/home/user/public_html/myprogram/.key
Environment Variable
It is a variable that persists within the environment.
Setting an Environment Variable
Add the variable to the ‘.bashrc’ file within the user environment wanted.
[user@server ~]$ echo “MY_PROGRAM_PATH=“/home/user/public_html/myprogram”” >> .bashrc
[user@server ~]$ echo “MY_PROGRAM_KEY=“/home/user/public_html/myprogram/.key”” >> .bashrc
Manually opening and editing the file with a file editor will also achieve the same result. I’ve used the echo command instead, as its quicker.
Once the environment variable is set, you’ll need to either log out and then back into the current bash shell or use the ‘source’ command to automatically use the variable without logging out of the current shell.
[user@server ~]$ source .bashrc