Variables
A shell exposes and uses environment variables to:
-
alter its behavior
-
alter the behavior of some external commands
-
store data
With bash, variables:
-
don’t have a data type in the first place
-
only exist in the current shell (if nothing is done to change that)
Variables: listing all of them
Internal commands export and set without parameters print the list of variables defined in the current shell
$> export
...
declare -x SHELL="/bin/bash"
declare -x SHLVL="1"
declare -x SSH_AGENT_PID="1338"
declare -x USER="luisgustavo.nardin"
...
$>
-
setalso prints user defined shell functions
Variables: storing data
Variables are a nice way to store data that is not too large
$> MY_VAR="I store text in this variable"
$> NUM=123
$> TMP_DIR=/tmp
$> SONG=The\ fool\ on\ the\ hill
$>
-
Assignment is made with the
=sign
no space allowed before or after this character! -
If the data you want to store in a variable contains special characters, you have to put your data in double quotes, or you may escape all the special characters by preceding them with
\(backslash) (kbd:[AltGr+8] or kbd:[⇪+⌥+L])
Variables: printing their values
To use a variable (to get its value), it must be prefixed with the $ character
$> echo $SHELL
/bin/bash
$> echo $MY_VAR
I store text in this variable
$> echo $SONG
The fool on the hill
$> echo $NUM
123
$>
-
echois a command that may be used to print the content (the value) of a variable
Variables: from the shell to other programs
To make a variable available to other commands launched by the shell or to subshells, you need to export it
$> bash -c "echo \$MY_VAR"
$> export MY_VAR
$> bash -c "echo \$MY_VAR"
I store text in this variable
$>
-
An export only affects the current shell and its subshells
-
It is not possible to export a variable to the parent shell
-
Once the variable is exported, any change to its value is automatically exported (however, the value in the already spawned subshells will not change)
Variables: from the shell to other programs
You can temporarily change the value or set a variable for the next call to an external command
$> echo $LANG
fr_FR.utf8
$> man 3 printf
## la page de manuel s'affiche en français
$> LANG=en_GB man 3 printf
## la page de manuel s'affiche, pour cette fois, en anglais
$> echo $LANG
fr_FR.utf8
$>
Variables: altering their contents
You may temporarily alter the content of a variable.
The main operators are:
-
#or##to remove a prefix -
%or%%to remove a suffix -
/or//to substitute a text with another
$> VAR="hello world.txt"
$> echo ${VAR#hel}
lo world.txt
$> echo ${VAR%.txt}
hello world
$> echo ${VAR/txt/c/}
hello world.c
$> echo ${VAR//o/00}
hell00 w00rld.txt
$>
More on that in the EXPANSION / Parameter Expansion section of the bash manual page.
Arithmetics
$> VAL=123
$> ((VAL++))
$> echo $VAL
124
$> echo $((VAL/2))
62
$> echo $VAL
124
$> RES=$((VAL/=2))
$> echo "RES=$RES, VAL=$VAL"
RES=62, VAL=62
$>
When a variable stores an integer, it is possible to make some computations on it using the (( … )) construct.
When the variable does not contain digits, bash make its 0.
References
-
"Unix Text Processing", Dale Dougherty and Tim O’Reilly, Hayden Books, 1987
https://www.oreilly.com/openbook/utp/ -
Christophe Blaess cheat sheets[FR]
https://www.blaess.fr/christophe/developpements/aides-memoires/-
Unix commands[FR]
https://www.blaess.fr/christophe/memo_commandes_unix.html -
Shell programming[FR]
https://www.blaess.fr/christophe/memo_programmation_shell.html
-
-
Rich’s sh (POSIX shell) tricks
https://www.etalabs.net/sh_tricks.html -
Bash Reference Manual
https://www.gnu.org/software/bash/manual/bashref.html
-
Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/ -
"Mastering Regular Expressions, 3rd Edition — Understand Your Data and Be More Productive", Jeffrey Friedl
https://www.oreilly.com/library/view/mastering-regular-expressions/0596528124/ -
"GAWK: Effective AWK Programming", Edition 4.1
http://www.gnu.org/software/gawk/manual -
Manual pages‡ :
bash(1),grep(1),regex(7),gawk(1)
‡ : read thoses pages on your own operating system, not on the Internet!