Set variable in shell linux

How to Set Environment Variables in Linux

In this tutorial, you will learn how to set environment variables in Ubuntu, CentOS, Red Hat, basically any Linux distribution for a single user and globally for all users. You will also learn how to list all environment variables and how to unset (clear) existing environment variables.

Environment variables are commonly used within the Bash shell. It is also a common means of configuring services and handling web application secrets.

It is not uncommon for environment specific information, such as endpoints and passwords, for example, to be stored as environment variables on a server. They are also used to set the important directory locations for many popular packages, such as JAVA_HOME for Java.

Setting an Environment 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.

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.

And so long as the variable has a value it will be echoed out. If no value is set then an empty line will be displayed instead.

Unsetting an Environment Variable

To unset an environment variable, which removes its existence all together, we use the unset command. Simply replace the environment variable with an empty string will not remove it, and in most cases will likely cause problems with scripts or application expecting a valid value.

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.

Listing All Set Environment Variables

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

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

Persisting Environment Variables for a User

When an environment variable is set from the shell using the export command, its existence ends when the user’s sessions ends. This is problematic when we need the variable to persist across sessions.

Читайте также:  Linux который заменит windows

To make an environment persistent for a user’s environment, we export the variable from the user’s profile script.

    Open the current user’s profile into a text editor

export JAVA_HOME=/opt/openjdk11

Adding the environment variable to a user’s bash profile alone will not export it automatically. However, the variable will be exported the next time the user logs in.

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

Export Environment Variable

Export is a built-in shell command for Bash that is used to export an environment variable to allow new child processes to inherit it.

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.

SHELL=/bin/zsh SHLVL=1 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.1pB5Pry8Id/Listeners TERM=xterm-256color 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.

Setting Permanent Global Environment Variables for All Users

A permanent environment variable that persists after a reboot can be created by adding it to the default profile. This profile is loaded by all users on the system, 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 you will find a list of files that are used to set environment variables for the entire system.

    Create a new file under /etc/profile.d to store the global environment variable(s). The name of the should be contextual so others may understand its purpose. For demonstrations, we will create a permanent environment variable for HTTP_PROXY.

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

Conclusion

This tutorial covered how to set and unset environment variables for all Linux distributions, from Debian to Red Hat. You also learned how to set environment variables for a single user, as well as all users.

Источник

Unix / Linux — Using Shell Variables

In this chapter, we will learn how to use Shell variables in Unix. A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data.

A variable is nothing more than a pointer to the actual data. The shell enables you to create, assign, and delete variables.

Variable Names

The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the underscore character ( _).

By convention, Unix shell variables will have their names in UPPERCASE.

The following examples are valid variable names −

Following are the examples of invalid variable names −

2_VAR -VARIABLE VAR1-VAR2 VAR_A!

The reason you cannot use other characters such as !, *, or is that these characters have a special meaning for the shell.

Defining Variables

Variables are defined as follows −

variable_name=variable_value

The above example defines the variable NAME and assigns the value «Zara Ali» to it. Variables of this type are called scalar variables. A scalar variable can hold only one value at a time.

Читайте также:  Dell inspiron n5050 linux

Shell enables you to store any value you want in a variable. For example −

Accessing Values

To access the value stored in a variable, prefix its name with the dollar sign ($) −

For example, the following script will access the value of defined variable NAME and print it on STDOUT −

#!/bin/sh NAME="Zara Ali" echo $NAME

The above script will produce the following value −

Read-only Variables

Shell provides a way to mark variables as read-only by using the read-only command. After a variable is marked read-only, its value cannot be changed.

For example, the following script generates an error while trying to change the value of NAME −

#!/bin/sh NAME="Zara Ali" readonly NAME NAME="Qadiri"

The above script will generate the following result −

/bin/sh: NAME: This variable is read only.

Unsetting Variables

Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable.

Following is the syntax to unset a defined variable using the unset command −

The above command unsets the value of a defined variable. Here is a simple example that demonstrates how the command works −

#!/bin/sh NAME="Zara Ali" unset NAME echo $NAME

The above example does not print anything. You cannot use the unset command to unset variables that are marked readonly.

Variable Types

When a shell is running, three main types of variables are present −

  • Local Variables − A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell. They are set at the command prompt.
  • Environment Variables − An environment variable is available to any child process of the shell. Some programs need environment variables in order to function correctly. Usually, a shell script defines only those environment variables that are needed by the programs that it runs.
  • Shell Variables − A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly. Some of these variables are environment variables whereas others are local variables.

Источник

Setting environment variables in Linux using Bash

What is the equivalent to the tcsh setenv function in Bash? Is there a direct analog? The environment variables are for locating the executable.

5 Answers 5

export VAR=value will set VAR to value. Enclose it in single quotes if you want spaces, like export VAR=’my val’ . If you want the variable to be interpolated, use double quotes, like export VAR=»$MY_OTHER_VAR» .

PS: no need for the double quotes in the last one. The shell does not perform word splitting for variable assignments.

Читайте также:  Proliant dl380 g5 linux

Just for the beginners. I had not understood it directly, here in normal English: interpolate «$X» means here that you already have a variable X that is filled with a value, e.g. «A», and you want to read out that value «A» first, and then assign this value as the value of VAR. You do not want VAR to be a string of «$X» here of course. And then the comment above seems also logical, that you will not even need the «». And for a direct assignment of «A», use ‘A’. Please correct me if I got it wrong.

The reason people often suggest writing

is that the longer form works in more different shells than the short form. If you know you’re dealing with bash , either works fine, of course.

Set a local and environment variable using Bash on Linux

Check for a local or environment variables for a variable called LOL in Bash:

el@server /home/el $ set | grep LOL el@server /home/el $ el@server /home/el $ env | grep LOL el@server /home/el $ 

Sanity check, no local or environment variable called LOL.

Set a local variable called LOL in local, but not environment. So set it:

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ env | grep LOL el@server /home/el $ 

Variable ‘LOL’ exists in local variables, but not environment variables. LOL will disappear if you restart the terminal, logout/login or run exec bash .

Set a local variable, and then clear out all local variables in Bash

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ exec bash el@server /home/el $ set | grep LOL el@server /home/el $ 

You could also just unset the one variable:

el@server /home/el $ LOL="so wow much code" el@server /home/el $ set | grep LOL LOL='so wow much code' el@server /home/el $ unset LOL el@server /home/el $ set | grep LOL el@server /home/el $ 

Local variable LOL is gone.

Promote a local variable to an environment variable:

el@server /home/el $ DOGE="such variable" el@server /home/el $ export DOGE el@server /home/el $ set | grep DOGE DOGE='such variable' el@server /home/el $ env | grep DOGE DOGE=such variable 

Note that exporting makes it show up as both a local variable and an environment variable.

Exported variable DOGE above survives a Bash reset:

el@server /home/el $ exec bash el@server /home/el $ env | grep DOGE DOGE=such variable el@server /home/el $ set | grep DOGE DOGE='such variable' 

Unset all environment variables:

You have to pull out a can of Chuck Norris to reset all environment variables without a logout/login:

el@server /home/el $ export CAN="chuck norris" el@server /home/el $ env | grep CAN CAN=chuck norris el@server /home/el $ set | grep CAN CAN='chuck norris' el@server /home/el $ env -i bash el@server /home/el $ set | grep CAN el@server /home/el $ env | grep CAN 

You created an environment variable, and then reset the terminal to get rid of them.

Or you could set and unset an environment variable manually like this:

el@server /home/el $ export FOO="bar" el@server /home/el $ env | grep FOO FOO=bar el@server /home/el $ unset FOO el@server /home/el $ env | grep FOO el@server /home/el $ 

Источник

Оцените статью
Adblock
detector