How to Display the Contents of a Variable in Unix
Unix users can store information temporarily on the console using environment variables. This is useful for creating scripts that automate a task for later. Some environment variables even have special meanings to the operating system, such as the "PATH" variable, which defines all the locations where the operating system will look for applications when a command is typed, and the "EDITOR" variable, which defines your preferred text editor within the terminal.
Instructions
-
-
1
Click the "terminal" icon on your desktop to open a terminal.
-
2
Create a new variable and set its contents. Simply type the following command:
VARIABLE_NAME="variable contents"
You can use any variable name you like, and set the variable contents to whatever you like. You can even use the back-tick symbol (just to the left of your 1 key on a U.S. keyboard) in place of quotes to set the variable to the output of another program. It is customary to give variables names that are in all uppercase, so they can be easily recognized, but any combination of English letters, numbers and dashes is acceptable.
There is one important exception to the above syntax: if you use the "csh" program as your shell, you will need to type instead:
setenv VARIABLE_NAME "variable contents"
-
-
3
Retrieve a list of all currently set environment variables by typing "env" into your shell.
-
4
Print the contents of any environment variable to your shell using the "echo" command and prefacing the variable name with a dollar sign, like so:
echo $VARIABLE_NAME
-
1
Tips & Warnings
Environment variables only last for as long as the current terminal is open. In order to preserve them across logins, most versions of Unix will check the user's home directory for a hidden file named ".bash_profile" or ".profile." This script is run every time the user opens a terminal and loads common environment variables.