Learn Linux - Chapter -5 - Shell Environment

Learn Linux - Chapter -5 - Shell Environment


This article was first published on Peerlyst you can also read here.

In the last chapter we learnt about getting help in Linux, you can read it here. Now we are going to learn about the shell environment in Linux.

The Linux shell environment can be best defined as the values set to certain variables which define the behaviour of a shell. These values set to the environment variables define the appearance of the prompt, home directory, working directory, and more. Some environment variables are set by the system, some by the user, some by the shell, and some variables are set by a program which run another program.

When a user logs in, he has a default shell which is specified in the file /etc/passwd. A majority of the distributions use bash shell. In this book when I say shell, I will be refering to the bash shell.

The /etc/passwd entry for me is

basheer:x:1000:1000:Basheer Ahmed Khan,,,:/home/basheer:/bin/bash

/bin/bash is the shell program. And /bin/bash is the value set to the environment variable SHELL.

Type echo $SHELL at the prompt

basheer@Linux-Box:~$ echo $SHELL
/bin/bash
basheer@Linux-Box:~$ 

An environment is created automatically when the user logs in. There are four profile scripts which are used to create this environment. In these files scripts are used and variables are defined, which creates an environment for the whole system and an environment for a specific user. These four files are etc/profile, /etc/bashrc, .profile or .bash_profile, and .bashrc.

The /etc/profile is used to set system wide environment variables for users shells.

The .profile or used to set specific users shells environment variables.

The /etc/bashrc file is used to set the system wide aliases and functions used by bash shell users.

The .bashrc like .profle is used to set specific users shells aliases and functions.

Normally the /etc/profile is modified during system updates, so it is not advisable to edit the /etc/profile. You can instead write scripts and save them in the directory /etc/profile.d as after running /etc/profile, bash runs the .sh files present in the /etc/profile.d.

A sample /etc/profile file is as below.

No alt text provided for this image

There are two types of shells, interactive shell and non-interactive shell.

An interactive shell is where you have a prompt. Where as a non-interactive shell is a shell where the user cannot interact with the shell. An example is a running bash script.

An example of how the values of the variables changes as you login as different users is below

basheer@Linux-Box:~$ echo $USER
basheer
basheer@Linux-Box:~$ sudo su
[sudo] password for basheer: 
root@Linux-Box:/home/basheer# echo $USER
root
root@Linux-Box:/home/basheer#exit
basheer@Linux-Box:~$ echo $USER
basheer
basheer@Linux-Box:~$

When you open the terminal, you are logged in as a user basheer. Hence when you print the value of USER, it prints basheer. 

When you login as the super user (root) and print the value os USER, it prints root.

And when you logout (exit) as root, USER value changes back to basheer.

Note: At the end of the prompt if the character is $, it is a normal user. And when the character at the end of the prompt is #, it is the root user.

Some of the common environment variables are below

  • PATH It is the search path for commands. It is a colon-seperated list of directories where the shell looks for commands.
  • USER The name of the logged in user.
  • HOME Home directory of the user.
  • UID Numeric user id of the current user.
  • TERM Display type
  • SHELL Name of the shell.
  • PWD Current working directory.

You can create or set a shell variable in the bash shell by typing a name and immediately followed by an equals sign and the value. There must not be any space in between. Variable names can have alphanumeric values and underscores and they are case sensitive.

Shell variables are available only to the processes which create them unless they are exported so that the child processes can also use them.

To remove a variable from the bash shell you have to use the unset command.

In this chapter you learnt about shell environment and the environment variables. In the next chapter we will be working with Files and Directories.

This series is an attempt to make aspiring cyber security professionals gain good knowledge of Linux.

要查看或添加评论,请登录

Basheer Ahmed Khan的更多文章

社区洞察

其他会员也浏览了