Key Linux Environment Variables in Linux and How to Find Them

Many environment variables are used by programs executed from the shell.  For example, a program may need to retrieve the shell environment variable $USER to determine the user name of the user currently running the program.  Get familiar with these common and important Linux environment variables:

$PATH - Executable search path.  It contains a list of directories the shell will look in for a program when it is called.
$PWD - Path to current working directory.  This variable changes whenever the current user changes the working directory thus it always points to the current working directory.
$TERM - Login terminal type (i.e., xterm).  It contains information about the terminal hardware used to log into the system.
$SHELL - Path to login shell (i.e., /bin/bash).
$HOME - Path to home directory (i.e., /home/oracle).
$USER - Username of the user (i.e., oracle).
$DISPLAY -  X display name (i.e., station2:0:0).
$EDITOR - Name of default editor (i.e., vi).
$VISUAL - Name of visual editor (i.e., emacs).

What is the difference between Windows PATH and Linux PATH?

This is a tricky Linux interview question.  The answer is about security.  In Unix/Linux, the current directory is not automatically in the $PATH.  This is deliberate decision and very important from a security perspective.  Having the current directory automatically be the first directory in the $PATH is a major security hole in a networked, multi-user system.  This hole has continued to plague Windows and is not possible to fix as the behavior is used by thousands of applications.  The security implication is that it is possible to execute a program or code that isn't intended.  This can happen in Windows using a DLL.

How to check environment variables in Linux?
  •  Use "echo" command to display variable value.
  • Use "printenv" to display all environment variables.  Use "grep" to filter.
  • Use "env" to list all environment variables (similar to printenv).
  • Use "set" command to check environment variables and other settings.
  • In bash shell, check "bash_profile" to see predefined environment variables.

No comments:

Post a Comment