Bash - (Environment) Variable

About

A variable is a parameters referenced by a name.

A variable (ie a name used to store data) in bash is called a parameter. A variable in bash is one of the three type of parameters.

A variable has:

  • a value
  • and zero or more attributes (such as integer, …). Attributes are assigned using the declare builtin command.

Syntax

Assignment

In the shell command language, a variable is a word consisting of the following parts:

[local] name=[value]

where:

If the variable has its integer attribute set, then value is evaluated as an arithmetic expression even if the $((...)) expansion is not used.

All values undergo the following expansion:

Is not performed:

Assignment statements may also appear as arguments to the:

Usage

  • ${VAR1}
  • $VAR1 is a simplified version of ${VAR1} where the boundary is not defined.

${VAR1} is a parameter expansion notation.

Example:

var1=Hello
$ echo ${var1}
Hello
$ echo "$var1_Nico"
# Blank
$ echo "${var1}_Nico"
Hello_Nico

Type

Scope

Example

$ varname=value
$ echo $varname
value

To create your own shell variables. First issue the command

newdir=$HOME/mynewdirectory

and then, regardless of what directory you are in, you can issue

cd $newdir

Shell Variable

The builtin, intern, system variable, reserved variable name, see: Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Management

Display

One

To see an environment variable, you make use of the echo command as :

[[email protected] ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin

or with the env command

$ env | grep PATH

All with env

To list the current values of all environment variables, issue the command

env
HOSTNAME=ebs121.localdomain
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.2.2 3886 22
SSH_TTY=/dev/pts/1
USER=root
.........

or

$ env | more

All with declare

To list the current values of all environment variables, issue the declare command with p

declare -p
declare -- BASH="/bin/bash"
declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:histappend:interactive_comments:login_shell:progcomp:promptvars:sourcepath"
declare -ir BASHPID
declare -A BASH_ALIASES='()'
declare -a BASH_ARGC='()'
..............

A subset

Thanks to the parameter expansion prefix, we can get a list of variable that begins with a prefix.

Example:

Prefix1=Prefix1Value
Prefix2=Prefix2Value
echo ${!Prefix*}
  • output:
Prefix1 Prefix2

Modify

For a session

If you set your variable in a script, you need to call it with the source command, otherwise they will not be available in the parent process

The syntax depends of your shell:

  • for the Bourne, Bash, or Korn shell:

To add /sbin to the path, type the export command in a console :

export PATH=$PATH:/sbin

or with two variable

$ TMP=/mount_point/tmp
$ TMPDIR=/mount_point/tmp
$ export TMP TMPDIR
  • For the C shell:
% setenv TMP /mount_point/tmp
% setenv TMPDIR /mount_point/tmp

The export or setenv word makes the variable to be available to all child sessions initiated from the current session

Permanently

You must change the shell startup script.

Delete

One

unset myEnvironmentVariable

All

With the env utility, you can start with a empty environment with the -i option.

env -i yourScript

Operations

Check if set

if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi

See Parameter check for more info.

Read-only

Documentation / Reference


Powered by ComboStrap