Unix Enviornment
Unix Enviornment
Simply put, environment variables are variables that are set up in your
shell when you log in. They are called “environment variables” because
most of them affect the way your Unix shell works for you. One points to
your home directory and another to your history file.
In this chapter, we will discuss in detail about the Unix environment. An important
Unix concept is the environment, which is defined by environment variables. Some
are set by the system, others by you, yet others by the shell, or any program that
loads another program.
A variable is a character string to which we assign a value. The value assigned
could be a number, text, filename, device, or any other type of data.
If you haven’t examined your environment variables in a while, you might be
surprised by how many of them are configured. An easy way to see how many have
been established in your account is to run this command:
$ env | wc -l
25
The env command (or printenv) will list all of the enviroment variables and
their values. Here’s a sampling:
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
HISTCONTROL=ignorespace
HISTFILE=/home/shs/.history
HISTIGNORE=history:pwd:man *:ls:ls *
HISTSIZE=100
HISTTIMEFORMAT=%h %d %H:%M:%S>
HOME=/home/shs
LANG=en_US.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
LESSOPEN=| /usr/bin/lesspipe %s
You can display a single variable with a simple echo command.
$ echo $HISTFILE
/home/shs/.history
For example, first we set a variable TEST and then we access its value using
the echo command −
$TEST="Unix Programming"
$echo $TEST
/etc/profile
profile
The process is as follows −
The shell checks to see whether the file /etc/profile exists.
If it exists, the shell reads it. Otherwise, this file is skipped. No error message
is displayed.
The shell checks to see whether the file .profile exists in your home
directory. Your home directory is the directory that you start out in after you
log in.
If it exists, the shell reads it; otherwise, the shell skips it. No error message is
displayed.
As soon as both of these files have been read, the shell displays a prompt −
$
This is the prompt where you can enter commands in order to have them executed.
Note − The shell initialization process detailed here applies to all Bourne type
shells, but some additional files are used by bash and ksh.
There are variables like PS1 and PS2 which are discussed in the next section.
PS1 stands for "Prompt String One" or "Prompt Statement One", the first
prompt string (that you see at a command line).
PS2 The secondary prompt string. ie for continued commands (those taking more than
one line). The default value is ‘> ’.
PS3 The value of this variable is used as the prompt for the select command. ie for input
into a running script. If this variable is not set, the select command prompts with ‘#? ’.
PS4 The value is the prompt printed before the command line is echoed when the -x
option is set. The first character of PS4 is replicated multiple times, as necessary, to
indicate multiple levels of indirection. The default is ‘+ ’.
PS1 and PS2 are from the original sh, PS3 and PS4 were added as part of bash
Your prompt will become =>. To set the value of PS1 so that it shows the working
directory, issue the command −
=>PS1="[\u@\h \w]\$"
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
The result of this command is that the prompt displays the user's username, the
machine's name (hostname), and the working directory.
There are quite a few escape sequences that can be used as value arguments for
PS1; try to limit yourself to the most critical so that the prompt does not overwhelm
you with information.
1 \t
Current time, expressed as HH:MM:SS
2 \d
Current date, expressed as Weekday Month Date
3 \n
Newline
4 \s
Current shell environment
5 \W
Working directory
6 \w
Full path of the working directory
7 \u
Current user’s username
8 \h
Hostname of the current machine
9 \#
Command number of the current command. Increases when a
new command is entered
10 \$
If the effective UID is 0 (that is, if you are logged in as root), end
the prompt with the # character; otherwise, use the $ sign
You can make the change yourself every time you log in, or you can have the
change made automatically in PS1 by adding it to your .profile file.
When you issue a command that is incomplete, the shell will display a secondary
prompt and wait for you to complete the command and hit Enter again.
The default secondary prompt is > (the greater than sign), but can be changed by
re-defining the PS2 shell variable −
Following is the example which uses the default secondary prompt −
$ echo "this is a
> test"
this is a
test
$
Environment Variables
Following is the partial list of important environment variables. These variables are
set and accessed as mentioned below −
1 DISPLAY
Contains the identifier for the display that X11 programs should use by
default.
2 HOME
Indicates the home directory of the current user: the default argument for
the cd built-in command.
3 IFS
Indicates the Internal Field Separator that is used by the parser for word
splitting after expansion.
4 LANG
LANG expands to the default system locale; LC_ALL can be used to
override this. For example, if its value is pt_BR, then the language is set to
(Brazilian) Portuguese and the locale to Brazil.
5 LD_LIBRARY_PATH
A Unix system with a dynamic linker, contains a colonseparated list of
directories that the dynamic linker should search for shared objects when
building a process image after exec, before searching in any other
directories.
6 PATH
Indicates the search path for commands. It is a colon-separated list of
directories in which the shell looks for commands.
7 PWD
Indicates the current working directory as set by the cd command.
8 RANDOM
Generates a random integer between 0 and 32,767 each time it is
referenced.
9 SHLVL
Increments by one each time an instance of bash is started. This variable
is useful for determining whether the built-in exit command ends the
current session.
10 TERM
Refers to the display type.
11 TZ
Refers to Time zone. It can take values like GMT, AST, etc.
12 UID
Expands to the numeric user ID of the current user, initialized at the shell
startup.
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$