UNIX Tutorial Seven

7.1 UNIX Variables

Variables are a way of passing information from the shell to programs and scripts when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Some are set by the system, others by you, yet others by the shell, or any program that loads another program. You can use variables as in any programming languages. There are no data types. A variable in bash can contain a number, a character, a string of characters. You have no need to declare a variable, just assigning a value to its reference will create it.

Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session. Variables are case sensitive but by convention, environment variables have UPPER CASE and shell variables have lower case names.

To set a variable in the shell simply assign a value.

% VARNAME="value"

To access the value of a variable use the $ symbol

% echo "VARNAME has the value "$VARNAME

Reference: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html

7.2 Environment Variables

An example of an environment variable is the OSTYPE variable. The value of this is the current operating system you are using. Type

% echo $OSTYPE

More examples of environment variables are

Finding out the current values of these variables.

The value of any variable can be found using the echo command.

To display all values of these variables we can use the printenv or env commands

% printenv | less

ENVIRONMENT variables are set globally using the export command, displayed and unset using the unset command.

% export VARNAME="value"

7.3 Shell Variables

Local variables are only available in the current shell. Using the set built-in command without any options will display a list of all variables (including environment variables) and functions. The output will be sorted according to the current locale and displayed in a reusable format.

SHELL variables can also be both set and displayed using the set command. They can be unset by using the unset command.

To show all values of these variables, type

% set | less

The set builtin is so complicated that it deserves its own section in the BASH manual - and thus should be avoided.

7.4 Setting the path

When you type a command, your path (or PATH) variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying "command: Command not found", this indicates that either the command doesn't exist at all on the system or it is simply not in your path.

For example, to run the program soshell, you either need to directly specify the soshell path (~/so2018/bin/soshell), or you need to have the directory ~/so2018/bin in your path.

You can add it to the end of your existing path (the $path represents this) by issuing the command:

% PATH=$PATH:~/so2018/bin

Test that this worked by trying to run units in any directory other that where units is actually located.

% cd; soshell

HINT: You can run multiple commands on one line by separating them with a semicolon.

To add this path PERMANENTLY, add the following line to your .bashrc AFTER the list of other commands.

export PATH=$PATH:~/so2018/bin)

7.5 Initalizing variables

Each time you login to a LINUX host, the system looks in your home directory for initialization files. Information in these files is used to set up your working environment. The Bash shell uses two following files. The files that start with a dot are hidden files

WARNING: NEVER put commands that run graphical displays (e.g. a web browser) in your .configuration files.

7.6 The history

The shell saves and records commands typed on the prompt, usually in the file .bash_history You can see a list of previous commands by typing

history

Previous commands can be accessed and executed using the !x notation

For more info see here You can adjust the number of previous commands that it stores in history. There are two options to manage, the HISTFILESIZE parameter configures how many commands are kept in the history file, while the HISTSIZE controls the number stored in memory for the current session.