0% found this document useful (0 votes)
94 views4 pages

The .Profile File:: (Amrood) $TEST "Unix Programming" (Amrood) $echo $TEST Unix Programming

Environment variables are used to define a shell's environment. Variables can store values like numbers, text, filenames, etc. Common variables include PATH, which specifies directories to search for commands, and PS1, which defines the primary prompt displayed by the shell. When a user logs in, the shell reads configuration files like /etc/profile and the user's .profile file to initialize environment variables and customize their shell environment.

Uploaded by

Pruthvi raj K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views4 pages

The .Profile File:: (Amrood) $TEST "Unix Programming" (Amrood) $echo $TEST Unix Programming

Environment variables are used to define a shell's environment. Variables can store values like numbers, text, filenames, etc. Common variables include PATH, which specifies directories to search for commands, and PS1, which defines the primary prompt displayed by the shell. When a user logs in, the shell reads configuration files like /etc/profile and the user's .profile file to initialize environment variables and customize their shell environment.

Uploaded by

Pruthvi raj K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

Avariable 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:

[amrood]$TEST="Unix Programming"
[amrood]$echo $TEST
Unix Programming

Note that environment variables are set without using $ sign but while accessing them we use $sign 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

The process is as follows:

1. The shell checks to see whether the file /etc/profile exists.


2. If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.
3. 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.
4. 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 .profile File:


The file /etc/profile is maintained by the system administrator of your UNIX machine and contains shell
initialization information required by all users on a system.

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

 The type of terminal you are using


 A list of directories in which to locate commands
 A list of variables effecting look and feel of your terminal.

You can check your .profile available in your home directory. Open it using vi editor and check all the
variables set for your environment.
Setting the Terminal Type:

Usually the type of terminal you are using is automatically configured by either the login orgetty programs.
Sometimes, the autoconfiguration process guesses your terminal incorrectly.

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:

[amrood]$TERM=vt100
[amrood]$

Setting the PATH:

When you type any command on command prompt, the shell has to locate the command before it can be
executed.

The PATH variable specifies the locations in which the shell should look for commands. Usually it is set as
follows:

[amrood]$PATH=/bin:/usr/bin
[amrood]$

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:

[amrood]$hello
hello: not found
[amrood]$

There are variables like PS1 and PS2 which are discussed in the next section.

PS1 and PS2 Variables:


The characters that the shell displays as your command prompt are stored in the variable PS1. You can
change this variable to be anything you want. As soon as you change it, it'll be used by the shell from that
point on.

For example, if you issued the command:

[amrood]$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.

Escape Sequence Description

\t Current time, expressed as HH:MM:SS.

\d Current date, expressed as Weekday Month Date

\n Newline.

\s Current shell environment.

\W Working directory.

\w Full path of the working directory.

\u Current user.s username.

\h Hostname of the current machine.

\# Command number of the current command. Increases with each new command
entered.

\$ If the effective UID is 0 (that is, if you are logged in as root), 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 > (the greater than sign), but can be changed by re-defining thePS2 shell
variable:

Following is the example which uses the default secondary prompt:

$ echo "this is a
> test"
this is a
test
$

Following is the example which re-define PS2 with a customized prompt:

$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->test"
this is a
test
$

You might also like