Ubuntu How To Set Environment Variable
Overview
In this tutorial, you volition learn how to set environment variables in Ubuntu, CentOS, Red Hat, basically whatever Linux distribution for a single user and globally for all users. You volition also learn how to listing all environment variables and how to unset (clear) existing environs variables.
Environment variables are commonly used within the Bash shell. It is besides a mutual ways of configuring services and handling spider web application secrets.
It is not uncommon for environment specific information, such equally endpoints and passwords, for case, to exist stored as environment variables on a server. They are also used to fix the of import directory locations for many popular packages, such as JAVA_HOME for Java.
Setting an Environment Variable
To set an surroundings variable the export control is used. We give the variable a name, which is what is used to admission information technology in shell scripts and configurations and then a value to hold any data is needed in the variable.
export Name=VALUE For example, to set the environment variable for the home directory of a transmission OpenJDK 11 installation, we would use something similar to the following.
export JAVA_HOME=/opt/openjdk11 To output the value of the surroundings variable from the crush, we use the echo command and prepend the variable'south name with a dollar ($) sign.
echo $JAVA_HOME And and so long as the variable has a value information technology will be echoed out. If no value is set and so an empty line will be displayed instead.
Unsetting an Surroundings Variable
To unset an surround variable, which removes its existence all together, we apply the unset command. Simply replace the environment variable with an empty string will not remove information technology, and in most cases will probable cause problems with scripts or application expecting a valid value.
To post-obit syntax is used to unset an surround variable
unset VARIABLE_NAME For example, to unset the JAVA_HOME environment variable, we would employ the post-obit command.
unset JAVA_HOME Listing All Set Environment Variables
To listing all environment variables, we simply use the set control without any arguments.
set An example of the output would expect something like to the following, which has been truncated for brevity.
Fustigate=/bin/fustigate BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath BASH_ALIASES=() BASH_ARGC=([0]="0") BASH_ARGV=() BASH_CMDS=() BASH_COMPLETION_VERSINFO=([0]="2" [1]="8") BASH_LINENO=() BASH_SOURCE=() BASH_VERSINFO=([0]="5" [ane]="0" [ii]="three" [three]="1" [4]="release" [5]="x86_64-pc-linux-gnu") BASH_VERSION='5.0.3(1)-release' COLUMNS=208 DIRSTACK=() EUID=g GROUPS=() HISTCONTROL=ignoreboth HISTFILE=/home/ubuntu/.bash_history HISTFILESIZE=2000 HISTSIZE=g HOME=/home/ubuntu HOSTNAME=ubuntu1904 HOSTTYPE=x86_64 IFS=$' \t\n' LANG=en_US.UTF-8 LESSCLOSE='/usr/bin/lesspipe %south %s' LESSOPEN='| /usr/bin/lesspipe %s' LINES=54 Persisting Surroundings Variables for a User
When an environment variable is prepare from the shell using the consign command, its being ends when the user's sessions ends. This is problematic when nosotros need the variable to persist beyond sessions.
To brand an surround persistent for a user's environment, nosotros export the variable from the user's profile script.
- Open the current user'southward profile into a text editor
vi ~/.bash_profile - Add the consign command for every environment variable y'all want to persist.
export JAVA_HOME=/opt/openjdk11 - Relieve your changes.
Calculation the environment variable to a user's bash profile alone will not export information technology automatically. However, the variable will be exported the next fourth dimension the user logs in.
To immediately apply all changes to bash_profile, apply the source command.
source ~/.bash_profile Export Environment Variable
Export is a built-in shell command for Bash that is used to export an environment variable to let new child processes to inherit it.
To consign a environment variable you run the export command while setting the variable.
export MYVAR="my variable value" We tin can view a consummate list of exported environment variables past running the export command without whatsoever arguments.
export SHELL=/bin/zsh SHLVL=1 SSH_AUTH_SOCK=/private/tmp/com.apple tree.launchd.1pB5Pry8Id/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.48.2 To view all exported variables in the electric current trounce y'all utilise the -p flag with export.
export -p Setting Permanent Global Environs Variables for All Users
A permanent environs variable that persists afterward a reboot can be created by adding it to the default profile. This profile is loaded past all users on the organisation, including service accounts.
All global profile settings are stored under /etc/profile. And while this file can be edited directory, it is actually recommended to store global environment variables in a directory named /etc/profile.d, where yous will discover a listing of files that are used to prepare environment variables for the unabridged system.
- Create a new file under /etc/profile.d to store the global environment variable(southward). The proper name of the should exist contextual so others may understand its purpose. For demonstrations, we volition create a permanent environment variable for HTTP_PROXY.
sudo affect /etc/profile.d/http_proxy.sh - Open up the default profile into a text editor.
sudo vi /etc/profile.d/http_proxy.sh - Add together new lines to export the environment variables
consign HTTP_PROXY=http://my.proxy:8080
consign HTTPS_PROXY=https://my.proxy:8080
export NO_PROXY=localhost,::1,.instance.com - Save your changes and get out the text editor
Decision
This tutorial covered how to set up and unset environment variables for all Linux distributions, from Debian to Carmine Lid. You as well learned how to set environs variables for a single user, likewise as all users.
Ubuntu How To Set Environment Variable,
Source: https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/
Posted by: acostabethertine.blogspot.com

0 Response to "Ubuntu How To Set Environment Variable"
Post a Comment