Variables+and+Shell+Expansions+Cheatsheet
Variables+and+Shell+Expansions+Cheatsheet
VARIABLES AND
SHELL EXPANSIONS
DEFINITION:
Parameters are entities that store values
POSITIONAL SPECIAL
VARIABLES
PARAMETERS PARAMETERS
VARIABLES
DEFINITION:
Variables are parameters that you can change the value of
2 TYPES OF VARIABLES
1 USER-DEFINED
VARIABLES
2 SHELL
VARIABLES
BASH SHELL VARIABLES
SETTING THE VALUE OF A VARIABLE
name=value
PATH List of directories that the shell should search for executable files
DEFINITION:
Parameter expansion is used to retrieve the value stored
in a parameter
2 ${parameter^^}
Convert all characters of the parameter to uppercase
3 ${parameter,}
Convert the first character of the parameter to lowercase
4 ${parameter,,}
Convert all characters of the parameter to lowercase
5 ${#parameter}
Display how many characters the variable’s value contains
Note: None of these alter the value stored in the parameter. They
just change how it is displayed after the expansion.
DEFINITION:
Command Substitution is used to directly reference the
result of a command
$(command)
ARITHMETIC EXPANSION
DEFINITION :
Arithmetic Expansion is used to perform mathematical
calculations in your scripts.
$(( expression ))
ARITHMETIC OPERATORS RANKED IN ORDER OF
PRECEDENCE (HIGHEST PRECEDENCE FIRST):
Exponentiation.
**
2**4 means 2 to the power
of 4, which is 16
Note: When two operators have the same precedence, the one furthest
to the left gets performed first.
THE BC COMMAND
echo “expression” | bc
Using the scale variable to control the number decimal places shown
DEFINITION:
Tilde expansion provides various shortcut for referencing
folders on the command line.
SYNTAX MEANING
Note: $PWD stores the current working directory and $OLDPWD stores the
previous working directory
BRACE EXPANSION
DEFINITION:
A way of automatically generating text according to a
certain pattern.
SYNTAX MEANING
{1,2,3,4,5} 12345
{1..5} 12345
{a..e}
abcde
{1..5..2}
135
Note: There should be no spaces around any commas or double dots (..)