Linux env¶
env command¶
Syntax:
env [OPTION]... [-][NAME=VALUE]... [COMMAND [ARG]...]
Options of env command
- Without any argument: print out a list of all environment variables
- Syntax:
env
- Syntax:
- -i or –ignore-environment or only –: runs a command with an empty environment
- Syntax:
env -i your_command
- Example:
env -i /bin/sh
- Syntax:
- -u or –unset: remove variable from the environment
- Syntax:
env -u variable_name
- Syntax:
- -0 or –null: End each output line with NULL, not newline
- Syntax:
env -0
- Syntax:
- –version: Display version information and exit
- Syntax:
env --version
- Syntax:
- –help: Display a help message and exit
- Syntax:
env --help
- Syntax:
Environment Variables¶
To set a global ENV¶
$ export NAME=Value
or
$ set NAME=Value
To set a local ENV¶
$ NAME=Value
To set user wide ENVs¶
~/.bashrc
, ~/.bash_profile
, ~/.bash_login
, ~/.profile
$ sudo vi ~/.bashrc
export NAME=Value
$ source ~/.bashrc
To set system wide ENVs¶
/etc/environment
, /etc/profile
, /etc/profile.d/
, /etc/bash
$ sudo -H vi /etc/environment
NAME=Value
Unset environment variables¶
$ unset NAME
or
$ NAME=''
Some commonly used ENVs in Linux¶
$USER: Gives current user's name.
$PATH: Gives search path for commands.
$PWD: Gives the path of present working directory.
$HOME: Gives path of home directory.
$HOSTNAME: Gives name of the host.
$LANG: Gives the default system language.
$EDITOR: Gives default file editor.
$UID: Gives user ID of current user.
$SHELL: Gives location of current user's shell program.
Example¶
echo $HOSTNAME
echo $SHELL
echo $HISTSIZE
echo $USER
echo $PATH
echo $PWD
echo $LANG
echo $HOME
echo $RANDOM