Leveling up in Bash #2: Variables & Expansions

Xavier Briand
Jan 30 · 3 min read

Second installment of my notes going through The Bash Guide.

First expansion is ~ that gets expanded in the pathname of the current user home directory. ~user expands to a specific user home directory.

So when cd ~, bash first expands ~ and then run cd <user's home folder path>.

Variable assignment: variable=value. No spaces around =, otherwise bash will try to run the command variable, with two parameters: = and value.

You prefix the variable with $ to expand it to its value:

> variable=value
> echo $variable
value

It is smarter to quote variable expansion to avoid bad surprises:

> filename="This is my filename.jpg"
> rm -v $filename
rm: this: No such file or directory
rm: is: No such file or directory
rm: my: No such file or directory
rm: filename.jpg: No such file or directory
> rm -v "$filename"

For pathnames, glob can be used (eg. *, ?, [0-9], [[:alpha:]]).
eg. rm -v *.jpg.

Bash has extended glob that can be enabled via: shopt -s extglob.
eg. rm -v *.jp?(e)g or rm -v *.@(jpg|jpeg).

Expansion operators

> var='this is the value'> echo "${var}s"
this is the values
> echo "${var#* }"
is the value
> echo "${var##* }"
value
> echo "${var% *}"
this is the
> echo "${var%% *}"
this
> echo "${var/ /-}"
this-is the value
> echo "${var// /-}"
this-is-the-value
> echo "${var/#* /That }"
That value
> echo "${var/% */ ends}"
this ends
> echo "${#var}"
17
> echo "${var:1}"
his is the value
> echo "${var:1:5}"
his i
> echo "${var: -5}"
value
> echo "${var^}"
This is the value
> echo "${var^^}"
THIS IS THE VALUE
> echo "${var^^[aeiu]}"
thIs Is thE vAlUE
> var='THIS IS THE VALUE'> echo "${var,}"
tHIS IS THE VALUE
> echo "${var,,}"
this is the value
> echo "${var,,[TSHVL]}"
thIs Is thE vAlUE

Command substitution

Sub-shells can also be expanded within a string:

> echo "This is the content of $filename: $(cat $filename)"

Alternative syntax is:

> echo "This is the content of $filename: `cat $filename`"

… and the reasons why you should not use it.

Environment & Shell variables

Environment variables are part of every processes. A child process get a copy of the parent’s environment variables.

> variable=value
> env | grep '^variable'
> export variable
> env | grep '^variable'
variable=value

When bash is used as a login shell, bash will lookup the .bash_profile file for custom environment variables. In this case, source ~/.bashrc needs to be part of the that file (bash will not source it).

Also:

Bash provides somes interesting variables like HOSTNAME, LANG, MACHTYPE, PWD, OLDPWD, IFS, PATH, PS1 and PS2.

Positional parameters

Expands process arguments.

> bash -vc 'echo $1: $2' -- foo bar
echo $1: $2
foo: bar

Special parameters

$@ expands the positional parameters as a list of separate arguments.

$# expands into the number of positional parameters available.

$_ expands to the last argument of the previous command.

$$ expands the pid of the current process

$- expands to the set of option flags that are currently active in the shell.

$! expands the pid of the last process that was started in the background

$? expands the exit code of the last synchronous process.

> bash -vc 'echo "$@"; echo "$_"; echo $#; ' -- foo bar baz
echo "$@"; echo "$_"; echo $#;
foo bar baz
baz
3

Arrays

> var=( One two 'the third' )> echo $var
One
> echo ${var[@]}
One Two the third
> echo ${var[0]}
One
> rm ${var[@]}
rm: One: No such file or directory
rm: two: No such file or directory
rm: the: No such file or directory
rm: third: No such file or directory
> rm "${var[@]}"
rm: One: No such file or directory
rm: two: No such file or directory
rm: the third: No such file or directory
> ( IFS=-; rm "${var[*]}" )
rm: One-two-the third: No such file or directory
> echo "${#var[@]}"
3
> echo "${#var[2]}"
9
> echo "${var[@]/ /-}"
One two the-third
> echo "${var[@]:1}"
two the third

My journey into Cybersecurity

My experience trailblazing the practice of Cybersecurity

Xavier Briand

Written by

50% solution finder at @ExperiencePoint / 50% endurance cyclist. Will train for food and burn it for adventures.

My journey into Cybersecurity

My experience trailblazing the practice of Cybersecurity

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade