Photo by Aaron Burden on Unsplash
Day 14 of my ALX Software Engineering Journey: Shell Init files, variables and expansions
Today I grasped the concept of initialization files, variables and expansions. Things are getting heated in the room but we’ve been reminded to keep on doing hard things!!
Expansion
Is a measure of the ability to express a command in a different form till the shell program runs it.
For example echo ls *
root@LAPTOP-8DCGIHH5:/alx# ls *
alx-low_level_programming:
0x00-hello_world README.md
alx-system_engineering-devops:
0x00-shell_basics 0x01-shell_permissions 0x02-shell_redirections 0x03-shell_variables_expansions README.md
root@LAPTOP-8DCGIHH5:/alx#
Above, the shell expands the *
into the name of the files in the current directory after the enter key is pressed. The *
was not read as *
but the expanded version was read which expands into the entire file in the directory.
Types of Expansions
- Pathname Expansions
A good example of a pathname expansion is the *
wild card symbol. It is used in pathnames to modify a command. For example
ls usr/bin/ *.h
The command above lists only the files that end with .h in the directory usr
and subdirectory bin
.
root@LAPTOP-8DCGIHH5:/# ls ./alx/alx*
./alx/alx-low_level_programming:
0x00-hello_world README.md
./alx/alx-system_engineering-devops:
0x00-shell_basics 0x01-shell_permissions 0x02-shell_redirections 0x03-shell_variables_expansions README.md
The command above lists all the files that are in the subdirectory of the alx
folder and begin with alx
. The *
symbol expands into files or folders that begin with alx
- Tilde Expansion (~)
Expands into the name of the user in the home directory. In the absence of a named user, it’s expanded into the directory of the current user.
root@LAPTOP-8DCGIHH5:~# echo ~
/root
- Arithmetic Expansion
This type of expansion allows arithmetic operations to be performed with expansions.
The format for arithmetic expansions : $((expression))
E.g echo $((2+2))
root@LAPTOP-8DCGIHH5:/# echo $((2+2))
4
e.g echo $((((6**3))*3))
root@LAPTOP-8DCGIHH5:/# echo $((((6**3))*3))
648
The above arithmetic expansion raised 6 to the power of 3 and further expands by multiplying the result by 3.
- Brace Expansion
This expansion is used to create multiple text strings from a pattern containing braces.
Patterns that are to be expanded using braces contain a preamble and trailing portion called a postscript. It can also contain a comma to separate integers or single characters.
For example
touch Dillibe{1..3}.txt
root@LAPTOP-8DCGIHH5:/# touch Dillibe{1..3}.txt
root@LAPTOP-8DCGIHH5:/# ls
Betty Dillibe2.txt alx boot etc file.txt home lib lib64 life mnt proc run snap sys tmp var
Dillibe1.txt Dillibe3.txt bin dev file filelist.txt init lib32 libx32 media opt root sbin srv test usr
Dillibe
is the preamble .txt
is the postscript.
- Parameter expansion
Is a method for transforming and manipulating the content of variables using the ${...}
syntax.
E.g echo $USER
root@LAPTOP-8DCGIHH5:/# echo $USER
root
The command above when typed, displays the name of the current user.
- Command substitution
Allows one to use the output of a command as an expansion.
e.g echo $()
Quoting
Is the process by which the shell suppresses an unwanted expansion
E.g echo my money is $100
root@LAPTOP-8DCGIHH5:~# echo my money is $100
my money is 00
Here's the result using quotes
root@LAPTOP-8DCGIHH5:~# echo my money is '$100'
my money is $100
Double Quotes
When a text is placed in a double quote it loses its special meaning and is treated as an ordinary character.
root@LAPTOP-8DCGIHH5:~# echo my money is "100$"
my money is 100$
A double quotation is important when naming a file and you want to add a space in between. Double quotes will suppress the changes and make the text appear as one.
Single quotes
are used to suppress all expansions as illustrated above.
Variables
Bash stores 2 kinds of variables namely; Global variable and Local variable
Global variables are environmental variables available in all shells.
E.g env printenv displays the environment
Local variable: is only available in the local Shell environment. It uses a set of built-in commands without any options, displays a list of all variables excluding environment variables and functions.
Variables can also be classified based on content:
String variables
Integer variables
Constant variable
Array variables
Creating variables
In the shell, variables are usually created using lowercase as a convention.
To set variable VARNAME = “value”
Exporting variables
To export local variables, the “export”
built-in command is used.
Variables exported are environment variables.
E.g export VARNAME = “value”
Shell initialization files
Are system or user configuration files. They are stored in the etc
directory.
e.g /etc/profile