[Unix-Linux] Unix Environment

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the Unix/Linux Tutorial!


Unix Environment

  • Environment is defined by environment variables → Some are set by the system, others by you, 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 could be a number, text, filename, device, or any other type of data.
$TEST="Unix Programming" // Set a variable TEST
$echo $TEST // Access its value using the echo command

// Output: Unix Programming
  • The environment var are set without using the $ sign, but we use the $ sign when accessing them.
  • These var hold their values until we exit the shell.


Initialization

When we log in to the system, the shell undergoes a phase called initialization to set up the environment.

This is usually a two-step process that involves the shell reading the following files:

  • /etc/profile
  • profile

The process:

  • 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.
  • 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.

The .profile File

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

The file .profile is under our control. We can add as much shell customization information as we want to this file.

The minimum set of information that we need to configure:

  • The type of terminal we are using.
  • A list of directories in which to locate the commands.
  • A list of variables affecting the look and feel of our terminals.


Setting the Terminal Type

The type of terminal is automatically configured by either the login and getty programs. Sometimes, the auto configuration process guesses our terminal incorrectly.

If terminal is set incorrectly, the output of the commands might look strange, or we might not be able to interact with the shell properly.

To make sure that this is not the case, the most users set their terminal to the lowest common denominator in the following way:

export TERM=dumb
echo $TERM //Output: dumb


Setting the PATH

When we type any command on the 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. Path variable is set as follows:

$PATH=/bin:/usr/bin
$

Each of the individual entries separated by the colon (:) are directories.

If we 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
$


PS1 and PS2 Variable

The PS1 and PS2 variables are environment variables in Unix and Linux systems that control the appearance and behavior of the command prompt in the shell.

  • PS1 (Primary Prompt String): Define the primary command prompt that is displayed when the shell is ready to accept commands.
  • PS2 (Secondary Prompt String): Define the secondary command prompt that is displayed when the shell expects additional input to complete a command.


PS1 (Primary Prompt String)

The shell uses the variable ‘PS1’ to determine the characters that are displayed as your command prompt. By default, the command prompt typically shows information like the current directory and the username.

If we want to set our command prompt to display just a simple ‘=>’ symbol, we can modify the ‘PS1’ variable as follows:

$PS1='=>'

After setting this value, the shell will display a ‘=>’ symbol as your command prompt. Any commands we enter will appear after this symbol.

Displaying a Command Prompt with Additional Information

=>PS1="[\u@\h \w]\$"
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
  • \u: Username
  • \h: Machine name (hostname)
  • \w: Current working directory

There are a few escape sequences that can be used as value arguments for PS1:

Sr.No Escape Sequence & Description
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

We can make the change ourselves every time we log in, or we can have the change made automatically in PS1 by adding it to our .profile file.


PS2 (Secondary Prompt String)

When we issue a command that is incomplete, the shell will display a secondary prompt and wait for us to complete the command and hit Enter again.

The default secondary prompt is > (the greater than sign), but can be changed by redefining the PS2 shell variable.

Default secondary prompt:

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

Re-define PS2 with a customized prompt:

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


Environment Variables

Sr.No Variable & Description
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

Example:

$ echo $HOME
/home/username

$ echo $DISPLAY
:0

$ echo $TERM
xterm-256color

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/home/username/bin
  • $HOME is set to /home/username, which is the home directory for the user “username”.
  • $DISPLAY is set to :0, indicating that an X server is running, and the current session is connected to the default display.
  • $TERM is set to xterm-256color, indicating that the terminal emulator supports 256 colors and is xterm-compatible.
  • $PATH is a list of directories separated by colons, where executable files can be found. The example shows a typical set of directories in the PATH variable.




Back to Top

See other articles in Category Unix-Linux

Leave a comment