Title: Variables
Course: CIS 121: Linux/UNIX I: Fundamentals ~ For IGNITE Review > Section: Learning Modules

Variables

You can also name variables and store information in them. The names must consist of alphanumeric or underscore characters and cannot start with a number. To create a variable, type the variable name, an equal sign and what you want to put in the variable. For example:

- my_name=Joe
- user=jsmith
- num_in_2015=27

You can input data into a variable with the read command. For example:

Set and Unset a Variable

You can set a variable to the result of a command by using back quotes. For example: today=`date`.

$today now holds the result of the "date" command at the moment it was executed.

You can unset a variable with "unset" or by setting the variable to nothing.

There are many environment or session variables that are automatically set for you. You can see all of your variables by typing: set.

One special variable is "$?". It holds the return code of the last executed command. Generally, if there are no errors, the return code from a command is a zero (0).

Script's Exit Value

The exit value of a script is the exit value of the last command in the script. To exit with a different value, use the exit command followed by an integer from 0 to 255.

exit 12