How to set environment Variables in Linux

In this article, we will see how to set environment Variables in Linux with examples.

We can use shell variables to store data, set configuration options and customize the shell environment under Linux.

Environment Variables in Linux used for following purposes:

  • Configure look and feel of shell.
  • Setup terminal settings depending on which terminal you’re using.
  • Set the search path such as JAVA_HOME, and ORACLE_HOME.
  • Set environment variables as needed by programs.
  • Run commands that you want to run whenever you log in or log out.
  • Setup aliases and/or shell function to automate tasks to save typing and time.
  • Changing bash prompt.
  • Setting shell options.

To get current Environment Variables:

Following is the list of all environmental variables in your entvironment.

We can get list of environment variable by using set, printenv or env commands:

BASH=/usr/bin/bash BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extquote:force_f ignore:globasciiranges:hostcomplete:interactive_comments:progcomp:promptvars:sou rcepath BASH_ALIASES=() BASH_ARGC=([0]="0") BASH_ARGV=() BASH_CMDS=() BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="5" [1]="0" [2]="3" [3]="1" [4]="release" [5]="x86_64-pc-linu x-gnu") BASH_VERSION='5.0.3(1)-release' COLUMNS=80 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus DIRSTACK=() EUID=127 GROUPS=() HISTFILE=/var/lib/postgresql/.bash_history HISTFILESIZE=500 HISTSIZE=500 HOME=/var/lib/postgresql HOSTNAME=mongodb1 HOSTTYPE=x86_64 IFS=$' \t\n' LANG=en_US.UTF-8 LESSCLOSE='/usr/bin/lesspipe %s %s' LESSOPEN='| /usr/bin/lesspipe %s' LINES=24 LOGNAME=postgres LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:c d=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37; 44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01 ;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t 7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lz o=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:* .tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01; 31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z =01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*. jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm= 01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*. tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01 ;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*. mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;3 5:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv= 01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.em f=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36: *.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00 ;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:' MACHTYPE=x86_64-pc-linux-gnu MAIL=/var/mail/postgres MAILCHECK=60 OLDPWD=/home/r2schools OPTERR=1 OPTIND=1 OSTYPE=linux-gnu PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/us r/local/games PIPESTATUS=([0]="0") PPID=7369 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' PS2='> ' PS4='+ ' PWD=/home/r2schools/scripts SHELL=/bin/bash SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:moni tor SHLVL=3 SSH_CLIENT='192.168.152.1 57035 22' SSH_CONNECTION='192.168.152.1 57035 192.168.152.135 22' SSH_TTY=/dev/pts/0 TERM=xterm UID=127 USER=postgres XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop XDG_RUNTIME_DIR=/run/user/1000 XDG_SESSION_CLASS=user XDG_SESSION_ID=50 XDG_SESSION_TYPE=tty _=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/l ocal/games command_not_found_handle () { if [ -x /usr/lib/command-not-found ]; then /usr/lib/command-not-found -- "$1"; return $?; else if [ -x /usr/share/command-not-found/command-not-found ]; then /usr/share/command-not-found/command-not-found -- "$1"; return $?; else printf "%s: command not found\n" "$1" 1>&2; return 127; fi; fi }

To get PATH variable:

echo $PATH postgres@mongodb1:/home/r2schools/scripts$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

How to set environment Variables in Linux

How to set present working directory as environment PATH variable:

Export is the command used to set environment variables in Linux.

export PATH=$PATH:$(pwd)

Check the this path is added or not

postgres@mongodb1:/home/r2schools/scripts$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/r2schools/scripts

How to set present working directory as environment PATH variable:

export PATH=$PATH:/home/r2schools/

To verify run the command echo $PATH.

Below are some of the most common environment variables:

USER – The current logged in user.
HOME – The home directory of the current user.
EDITOR – The default file editor to be used. This is the editor that will be used when you type edit in your terminal.
SHELL – The path of the current user’s shell, such as bash or zsh.
LOGNAME – The name of the current user.
PATH – A list of directories to be searched when executing commands. When you run a command the system will search those directories in this order and use the first found executable.
LANG – The current locales settings.
TERM – The current terminal emulation.
MAIL – Location of where the current user’s mail is stored.

How to create temporary user in Linux

In this article, we will see how to create temporary user in Linux. We can create user using useradd command by specifying expiry date. After expire date reached, the Linux user disabled.

-e, --expiredate EXPIRE_DATE 1. The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD. 2. If not specified, useradd will use the default expiry date specified by the EXPIRE variable in /etc/default/useradd, or an empty string (no expiry) by default.

Continue reading How to create temporary user in Linux

How to set or change Linux User Password

In this article, we will see how to set or change Linux User Password. We can set the password for new and change password for existing user using Linux passwd command. Passwords do not display to the screen when you enter them.

Linux system administrator (sysadmin) you can change password for any users on Linux server.

How to set password for Linux user

By default, when we create user in Linux with useradd, its doesnt have any password. We have to set password.
Continue reading How to set or change Linux User Password

How to make tar file in Linux

In this article, we will see how to make tar file in Linux. Tar stands for tape archive which is used to create and extract archive files.

Syntax:

tar [options] [archive-file] [file or directory to be archived]

Options:
C:- Create Archive
X:- Extract Archive(decompress)
v:- Verbose Output
f:- Names the tar file.
z:- zip, tells tar command that create tar file using gzip
Continue reading How to make tar file in Linux