Bash - (Environment) Variable

1 - 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.

3 - Syntax

3.1 - 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:

3.2 - 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

3.3 - Type

3.4 - Scope

4 - 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

5 - Shell Variable

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

6 - Management

6.1 - Display

6.1.1 - 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

6.1.2 - 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

6.1.3 - 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='()'
..............

6.1.4 - 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

6.2 - Modify

6.2.1 - 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

6.2.2 - Permanently

You must change the shell startup script.

6.3 - Delete

6.3.1 - One

unset myEnvironmentVariable

6.3.2 - All

With the env utility

env -i yourScript

6.4 - Operations

6.4.1 - 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.

6.5 - Read-only

7 - Documentation / Reference

lang/bash/variable.txt · Last modified: 2018/09/16 12:42 by gerardnico