Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

UNIX environment


May 23, 2021 UNIX Getting started


Table of contents


UNIX environment

An important concept in UNIX is the environment, which is defined by environment variables. Some environment variables are set by the system, some by the user, and some by the shell, or by any program that loads another program.

A variable is a string of characters, and we assign it a value. The value assigned to a variable can be a number, text, file name, device, or any other type of data.

For example, first, we set a variable with the name TEST, and then we use the echo command to see its value:

$TEST="Unix Programming"
$echo $TEST
Unix Programming

Note that setting environment variables does not use $ symbol, but when accessing them, we use the $ as a prefix. These variables save their values until we exit the shell.

When you log on to the system, the shell is initialized, during which various environment variables are set. This typically involves a two-step process in which the shell reads the following files:

  • /etc/profile
  • profile

The process is as follows:

  1. The shell program checks for the existence of the /etc/profile file.
  2. If the file exists, the shell program reads the file. O therwise, the file is skipped. No error messages are displayed.
  3. The shell program checks to see if the .profile file exists below your root. Your root is the directory you entered after you signed in.
  4. If the file exists, the shell program reads it. Otherwise, the shell program skips it and does not display any error messages.

Once the two file reads are complete, the shell displays a pending input command:

    $

This is a hint, after which you can enter commands to execute.

Note: The detailed process of shell initialization typically takes advantage of the Bourne shell, but some other file processing takes advantage of bash and ksh shell programs.

The .profile file

The /etc/profile file is maintained by UNIX's system administrator and contains shell initialization information that can be viewed by any user in any system.

If you have permission to the .profile file operation, you can add as much custom shell information as you want to in the file.

  • The type of terminator you are using
  • A list of files that the command exists on
  • Some columns of variables set the effect of your terminal display

You can view the .profile file below your root. Open it with the vi editor to see all the environment variables set in it.

Sets the type of terminator

Usually the type of terminal you are using is automatically configured by the login or getty program. Sometimes, the automatic configuration process speculates that your terminal type is incorrect.

If your terminal settings are wrong, the output of the command may look strange, or you may not be able to interact with the shell properly.

To ensure that this is not the case, most users' terminals have at least the same features as follows:

$TERM=vt 100
$

Set the PATH variable

When you enter any command at the command prompt, shells can execute commands only if they determine the directory in which the commands are located.

Shell is the directory in which the command is found in the environment variable PATH. Typically, it is set as follows:

$PATH=/bin:/usr/bin
$

Here each by the colon,:, the separate entity is the directory. If you ask the shell to execute a command, but it cannot find the path where any command is located in the PATH environment variable, a message like this appears:

$hello
hello: not found
$

There are also variables like PS1 and PS2, which are described in the next section.

PS1 and PS2 variables

The command prompts that the shell displays to you are stored in variable PS1. Y ou can change this variable into any character you want. As soon as you change it, it will work from the time you change.

For example, if you enter the following command:

$PS1='=>'
=>
=>
=>

Your prompt input will change to . Set the value of PS1, let it display the working directory, and enter the following 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 user's user name, machine name (host name), and working directory are displayed.

There are quite a few escape sequences that can be used as parameters for PS1, so try to focus on the most critical parts and don't let the information below put too much pressure on you.

Escape sequence Describe
\t Represents the current time as HH:MM:SS
\d Represents the current date as the week, month, and day
\n A new line.
\s The current environment variable.
\W The working directory.
\w The full path to the working directory.
\u The user name of the current user.
\h The host name of the current machine.
\# The number of the current command. Add 1 to each command number entered.
\$ If a valid UID is 0 (that is, if you log on as a root user), the command prompt becomes a hashtag, otherwise the prompt is $.

You can make .profile above each time you log on by modifying the .profile file. This automatically changes the value of PS1 each time you log on.

When you enter an incomplete command, the shell displays a command input again, waiting for you to complete the command again and return.

The default secondary prompt is greater than the sign, but can be changed by setting the PS2 variable:

The following example uses the default secondary prompt:

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

Here's an example of customizing inputs by redefining PS2 variables:

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

Environment variables

The following is a list of some important environmental variables. These variables will be set and accessed in the manner mentioned above:

Variable Describe
DISPLAY Contains the identifier of the display device, whose value is X11 by default.
HOME Indicates the current user's root, and the cd command is built into the default parameters.
Ifs Indicates the field separator used within the system, which is typically used in parser split words.
Lang LANG extends the system's default language: LC_ALL can be used to override this variable. For example, if its value is pt_BR, the language of the system is set to Brazilian Portuguese and the region is set to Brazil.
LD_LIBRARY_PATH Many UNIX system dynamic linkers, which contain a list of directories separated by colons, search for shared objects before searching for other directories during the dynamic connector build process image.
PATH The search path for the command. It is a series of directories separated by colons, which is where the shell looks for commands.
Pwd The current working directory, set by the cd command.
RANDOM Each reference generates a random integer in the range of 0 to 32,767.
SHLVL This value is added 1 each time a bash instance is launched. This variable is useful for determining whether the built-in exit command terminates the current session.
TERM The type of display.
TZ The time zone. It can be assigned GMT, AST, etc.
Uid The numeric type identifies the current user, which is initialized when the shell starts.

Here are a few simple examples showing several environment variables:

$ echo $HOME
/root
]$ echo $DISPLAY
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$