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
[bob@bob ~]$ MY_PROGRAM_PATH=/home/bob/public_html/myprogram
[bob@bob ~]$ MY_PROGRAM_KEY=/home/bob/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.
[bob@bob ~]$ echo $MY_PROGRAM_PATH
[bob@bob ~]$ echo $MY_PROGRAM_KEY
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.
[bob@bob ~]$ echo “MY_PROGRAM_PATH=“/home/bob/public_html/myprogram”” >> .bashrc
[bob@bob ~]$ echo “MY_PROGRAM_KEY=“/home/bob/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.
[bob@bob ~]$ source .bashrc