Unix Environment
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.
For example, first we set a variables TEST and then we access its value using echo command:
$TEST="Unix Programming"
$echo $TEST
Unix Programming
Note that environment variables are set without using signbutwhileaccessingthemweusesign as prefix.
These variables retain their values until we come out shell.
When you login to the system, the shell undergoes a phase called initialization to set up various
environment. This is usually a two step process that involves the shell reading the following files −
/etc/profile
profile
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 execute.
Note − The shell initialization process detailed here applies to all Bourne type shells, but some
additional files are used by bash and ksh.
The file .profile is under your control. You can add as much shell customization information as
you want to this file. The minimum set of information that you need to configure includes
You can check your .profile available in your home directory. Open it using vi editor and check all
the variables set for your environment.
If your terminal is set incorrectly, the output of commands might look strange, or you might not be
able to interact with the shell properly.
To make sure that this is not the case, most users set their terminal to the lowest common
denominator as follows −
$TERM=vt100
$
The PATH variable specifies the locations in which the shell should look for commands. Usually it is
set as follows −
$PATH=/bin:/usr/bin
$
Here each of the individual entries separated by the colon character, :, are directories. If you
request the shell to execute a command and it cannot find it in any of the directories given in the
PATH variable, a message similar to the following appears −
$hello
hello: not found
$
There are variables like PS1 and PS2 which are discussed in the next section.
$PS1='=>'
=>
=>
=>
Your prompt would 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.
\n Newline.
\W Working directory.
\$ If the effective UID is 0 thatis, ifyouareloggedinasroot, end the prompt with the
# character; otherwise, use the $.
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 > thegreaterthansign, but can be changed by re-defining the PS2
shell variable −
$ echo "this is a
> test"
this is a
test
$
$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->test"
this is a
test
$
Environment Variables
Following is the partial list of important environment variables. These variables would be set and
accessed as mentioned above −
Variable Description
DISPLAY Contains the identifier for the display that X11 programs should use by
default.
HOME Indicates the home directory of the current user: the default argument
for the cd built-in command.
IFS Indicates the Internal Field Separator that is used by the parser for word
splitting after expansion.
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.
TZ Refers to Time zone. It can take values like GMT, AST, etc.
UID Expands to the numeric user ID of the current user, initialized at shell
startup.
$ echo $HOME
/root
]$ echo $DISPLAY
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$
Loading [MathJax]/jax/output/HTML-CSS/jax.js