Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Set and List Environment Variables in Linux - DesignLinux

Jun 03 2020
designlinux 0 Comments
How to Set and List Environment Variables in Linux

In Linux based systems you can set dynamic named values as environment variables. These values are stored within system and that are used by shells. An environment variable is a variable with a name and an associated value. In this article, we will show you how to set and use environment variables.

Environment and Shell Variables#

The basic format for the variable are as following:

KEY="Some value"
  • Environment variables names should in UPPER CASE. Remember that the names of the variables are case-sensitive.
  • To assign multiple values you should use : colon as separator.
  • There should not have space before or after the = equal sign

There are two types of variables, Environment variable and Shell variable:

  • Environment variables are variables those are available system-wide and are inherited by all spawned child processes and shells.
  • Shell variables are variables that apply only to the current shell instance. Each shell such as zsh and bash, has its own set of internal shell variables.

List Environment and Shell Variables#

Following commands are available that allow you to list environment variables in Linux:

  • env – This command allows you to run another program in a custom environment without modifying the current one. It will print a list of the current environment variables when used without an argument.
  • printenv – The command prints the specified environment or all variables.
  • set – It will set or unset shell variables. It will list all the variables including environment and shell variables, when used without an argument.
  • unset – Used to delete shell and environment variables.
  • export – The command sets environment variables.

The printenv is mostly used to print the environment variables. If any argument is passed then it will print only value of the variable otherwise it will print a list of all environment variables.

For instance, to print the value of the USER environment variable, you would type:

printenv USER

It will show output as following:

tecnstuff

You can print multiple variables simultaneously separating by space:

printenv USER HOME
tecnstuff
/home/tecnstuff

If you run the printenv or env command without any argument then it will show a list of all the variables.

printenv
LESSCLOSE=/usr/bin/lesspipe %s %s
LANG=en_US
XDG_SESSION_ID=154
USER=tecnstuff
PWD=/home/tecnstuff
HOME=/home/tecnstuff
SSH_CLIENT=53.21.53.1 6366 22
XDG_DATA_DIRS=/usr/local/share:/usr/share:/var/lib/snapd/desktop
SSH_TTY=/dev/pts/1
MAIL=/var/mail/tecnstuff
TERM=cygwin
SHELL=/bin/bash
SHLVL=1
LOGNAME=tecnstuff
XDG_RUNTIME_DIR=/run/user/1000
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
LESSOPEN=| /usr/bin/lesspipe %s
_=/usr/bin/printenv

Following are some of the most common environment variables:

  • USER – It will show the current logged in user.
  • HOME – It’s home directory of the current user.
  • EDITOR – It will show name of default file editor.
  • SHELL – Display the path of the current user’s shell, such as bash or zsh.
  • LOGNAME – The name of the current user.
  • PATH – It is a list of directories. 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 – It displays the location of the current user’s mail storage.

To get the list of all variables and shell functions you should use set command:

set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_VERSINFO=([0]="2" [1]="8")
BASH_LINENO=()
BASH_SOURCE=()

The above command will show a large list in output so you can pipe the output to the less command:

set | less

Set Shell and Environment Variables#

To create a new shell variable with name FIRST_VAR and set TecNStuff, you would type:

FIRST_VAR='TecNStuff'

Verify that variable is set using echo $FIRST_VAR or you can filter output using grep command:

echo $FIRST_VAR
TecNStuff

Now we will use printenv command to check whether the variable is environment or shell variable.

printenv FIRST_VAR

The output is blank, that means it’s not a environment variable.

Conclusion#

In this article, we have shown you how to set and list environment and shell variables.

If you have any question or feedback, feel free to leave comment below.

Related

Tags: terminal

How to Install Tomcat 9 on Ubuntu 20.04

Prev Post

Zorin OS 15 – An Ultimate Linux Desktop Designed for Windows and macOS Users

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 1
  • 493
  • 612,672

DesignLinux.com © All rights reserved

Go to mobile version