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:
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:
With the read command
read -r var1 var2 var3 <<< "val1 val2 val3"
echo $var1
echo $var2
echo $var3
val1
val2
val3
${VAR1} is a parameter expansion notation.
Example:
var1=Hello
echo ${var1}
Hello
echo "$var1_Nico"
# Blank
echo "${var1}_Nico"
Hello_Nico
See:
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
The builtin, intern, system variable, reserved variable name, see: Bash - (Builtin|Intern|System|Reserved|Shell) variable name
To see an environment variable, you make use of the echo command as :
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
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
To list the current values of all environment variables, execute the declare command
declare
To list all names with their definitions, 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='()'
..............
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*}
Prefix1 Prefix2
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:
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
% 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
You must change the shell startup script.
unset myEnvironmentVariable
With the env utility, you can start with a empty environment with the -i option.
env -i yourScript
See Bash - Parameter
if [ -z ${var+x} ]; then echo "var is unset"; else echo "var is set to '$var'"; fi
See Parameter check for more info.
Bash - Declare (Variable declaration and attribute)
declare -r variableName