Shell script to set environment variables permanently

Asked
Active3 hr before
Viewed126 times

8 Answers

90%

When you do this, you're creating a new PATH variable by appending a directory to the current PATH variable, $PATH. A colon (:) separates PATH entries.,The change is temporary for the current session. It isn't permanent because it's not entered into the .bashrc file. To make the change permanent, enter the command PATH=$PATH:/opt/bin into your home directory's .bashrc file.,Temporarily change your PATH by entering the following command to add /opt/bin:,The PATH variable contains the search path for executing commands and scripts. To see your PATH, enter:

If you want to see your environment variables, use the env command and look for the words in all caps in the output's far left. These are your environment variables, and their values are to the right:

 $ env
 LS_COLORS = (long output)
 LANG = en_US.UTF - 8
 HISTCONTROL = ignoredups
 HOSTNAME = rhel8t
 XDG_SESSION_ID = 5
 USER = khess
 SELINUX_ROLE_REQUESTED =
    PWD = /home/khess
 HOME = /home/khess
 SSH_CLIENT = 192.168 .1 .94 53911 22
 SELINUX_LEVEL_REQUESTED = XDG_DATA_DIRS = /home/khess / .local / share / flatpak / exports / share: /var/lib / flatpak / exports / share: /usr/local / share: /usr/share
 SSH_TTY = /dev/pts / 1
 MAIL = /var/spool / mail / khess
 TERM = xterm - 256 color
 SHELL = /bin/bash
 SELINUX_USE_CURRENT_RANGE =
    SHLVL = 1
 LOGNAME = khess
 DBUS_SESSION_BUS_ADDRESS = unix: path = /run/user / 1000 / bus
 XDG_RUNTIME_DIR = /run/user / 1000 PATH = /home/khess / .local / bin: /home/khess / bin: /usr/local / bin: /usr/bin: /usr/local / sbin: /usr/sbin: /opt/bin
 HISTSIZE = 1000
 LESSOPEN = || /usr/bin / lesspipe.sh % s _ = /usr/bin / env
load more v
88%

You can add it to the file .profile or your login shell profile file (located in your home directory).,To change the environmental variable "permanently" you'll need to consider at least these situations:,How to save these variables permanently ?, 7 .profile is in your home directory not /etc/ – Kiwy Feb 28 '14 at 13:45

  1. Bash as login shell will load /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile in the order
  2. Bash as non-login interactive shell will load ~/.bashrc
  3. Bash as non-login non-interactive shell will load the configuration specified in environment variable $BASH_ENV
$EDITOR~/.profile
#add lines at the bottom of the file:
   export LD_LIBRARY_PATH = /usr/lib / oracle / 11.2 / client64 / lib
export ORACLE_HOME = /usr/lib / oracle / 11.2 / client64

zsh

$EDITOR~/.zprofile
#add lines at the bottom of the file:
   export LD_LIBRARY_PATH = /usr/lib / oracle / 11.2 / client64 / lib
export ORACLE_HOME = /usr/lib / oracle / 11.2 / client64

ksh

$EDITOR~/.profile
#add lines at the bottom of the file:
   export LD_LIBRARY_PATH = /usr/lib / oracle / 11.2 / client64 / lib
export ORACLE_HOME = /usr/lib / oracle / 11.2 / client64

bourne

$EDITOR~/.profile
#add lines at the bottom of the file:
   LD_LIBRARY_PATH = /usr/lib / oracle / 11.2 / client64 / lib
ORACLE_HOME = /usr/lib / oracle / 11.2 / client64
export LD_LIBRARY_PATH ORACLE_HOME

csh or tcsh

$EDITOR~/.login
#add lines at the bottom of the file:
   setenv LD_LIBRARY_PATH / usr / lib / oracle / 11.2 / client64 / lib
setenv ORACLE_HOME / usr / lib / oracle / 11.2 / client64
load more v
72%

Set an Environment Variable in Linux Permanently,1. To set permanent environment variables for a single user, edit the .bashrc file:,How to Unset an Environment Variable,How to Export an Environment Variable

Structurally, environment and shell variables are the same – both are a key-value pair, separated by an equal sign.

VARIABLE_NAME = value
load more v
65%

To following syntax is used to unset an environment variable,For example, to unset the JAVA_HOME environment variable, we would use the following command.,To list all environment variables, we simply use the set command without any arguments.,To export a environment variable you run the export command while setting the variable.

To set an environment variable the export command is used. We give the variable a name, which is what is used to access it in shell scripts and configurations and then a value to hold whatever data is needed in the variable.

export NAME = VALUE

For example, to set the environment variable for the home directory of a manual OpenJDK 11 installation, we would use something similar to the following.

export JAVA_HOME = /opt/openjdk11

To output the value of the environment variable from the shell, we use the echo command and prepend the variable’s name with a dollar ($) sign.

echo $JAVA_HOME

To following syntax is used to unset an environment variable

unset VARIABLE_NAME

For example, to unset the JAVA_HOME environment variable, we would use the following command.

unset JAVA_HOME

To list all environment variables, we simply use the set command without any arguments.

set

An example of the output would look something similar to the following, which has been truncated for brevity.

BASH = /bin/bash
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" [1] = "0" [2] = "3" [3] = "1" [4] = "release" [5] = "x86_64-pc-linux-gnu")
BASH_VERSION = '5.0.3(1)-release'
COLUMNS = 208
DIRSTACK = ()
EUID = 1000
GROUPS = ()
HISTCONTROL = ignoreboth
HISTFILE = /home/ubuntu / .bash_history
HISTFILESIZE = 2000
HISTSIZE = 1000
HOME = /home/ubuntu
HOSTNAME = ubuntu1904
HOSTTYPE = x86_64
IFS = $ ' \t\n'
LANG = en_US.UTF - 8
LESSCLOSE = '/usr/bin/lesspipe %s %s'
LESSOPEN = '| /usr/bin/lesspipe %s'
LINES = 54

vi~/.bash_profile

export JAVA_HOME = /opt/openjdk11

To immediately apply all changes to bash_profile, use the source command.

source~/.bash_profile

To export a environment variable you run the export command while setting the variable.

export MYVAR = "my variable value"

We can view a complete list of exported environment variables by running the export command without any arguments.

export
SHELL = /bin/zsh
SHLVL = 1
SSH_AUTH_SOCK = /private/tmp / com.apple.launchd .1 pB5Pry8Id / Listeners
TERM = xterm - 256 color
TERM_PROGRAM = vscode
TERM_PROGRAM_VERSION = 1.48 .2

To view all exported variables in the current shell you use the -p flag with export.

export -p

sudo touch / etc / profile.d / http_proxy.sh

sudo vi / etc / profile.d / http_proxy.sh

export HTTP_PROXY = http: //my.proxy:8080

export HTTPS_PROXY = https: //my.proxy:8080

export NO_PROXY = localhost, ::1, .example.com
load more v
75%

/etc/environment If you want your environment variable in every window or application (not just terminal window) you have to edit this file. Add the following command at the end of this file: DISPLAY=0 Note that in this file you do not have to write export command ,When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file:,~/.bashrc When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file: export DISPLAY=0 ,I've written a simple script for these procedures to do all those work. You just have to set the name and value of your environment variable.

When you open any terminal window this file will be run. Therefore, if you wish to have a permanent environment variable in all of your terminal windows you have to add the following line at the end of this file:

export DISPLAY = 0
load more v
40%

If you are doing things via script, one easy way to set environment variable permanently is put below statement in your script,,To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work., This is a correct answer for certain cases. All sessions, whether user shell sessions or not will get the value of variables set here. It is important to note that while this may look like a shell script, it is not -- it allows you only to set environment variables (vs. using shell functions like if or test) – Tom Harrison Jr Nov 9 '16 at 18:44 ,I'm running Ubuntu 11.04. I use the terminal to start a bash session, and I want to add an environment variable:

To set variable only for current shell:

VARNAME = "my value"

To set it for current shell and all processes started from current shell:

export VARNAME = "my value"
# shorter, less portable version

To set it permanently, and system wide (all users, all processes) add set variable in /etc/environment:

sudo - H gedit / etc / environment

This file only accepts variable assignments like:

VARNAME = "my value"
load more v
22%

To set permanent environment variables in Linux Mint follow next steps:,In this short tutorial, I’ll show you how to set environment variables permanently in Linux Mint.,To list all environment variables in Linux Mint you can use the next command:,For tests you can create session variables. The session environment variable is created by writing in terminal:

For tests you can create session variables. The session environment variable is created by writing in terminal:

export RSTUDIO_PANDOC = /usr/lib / rstudio / bin / pandoc
load more v
60%

In order to set a permanent environment variable in Bash, you have to use the export command and add it either to your “.bashrc” file (if this variable is only for you) or to the /etc/environment file if you want all users to have this environment variable.,In this tutorial, you learnt how you can easily set environment variables in Bash using the export command.,In some cases, you may need to set a specific environment variable to the result of a command on your server.,Similarly, you can use the “printenv” command in order to print the value of your environment variable.

For example, to assign the value “abc” to the variable “VAR“, you would write the following command

$
export VAR = abc
load more v

Other "undefined-undefined" queries related to "Shell script to set environment variables permanently"