shell variable expansion
Have you ever found yourself writing a shell script and wanting to incorporate the value of a variable into a command or string? Or maybe you've wanted to manipulate the value of a variable before using it in your script. If so, then you'll want to learn about shell variable expansion. Lets take a deep dive into this powerful feature of the shell and explore some of the various ways you can use it to make your scripts more flexible and efficient
Read or watch.
printenv
set
unset
export
alias
unalias
.
source
printf
By the end of this project, you should be able to explain to anyone Without the help of google the following concepts
- What happens when you type $
ls -l *.txt
- What are the
/etc/profile
file and the/etc/profile.d
directory - What is the
~/.bashrc
file
- What is the difference between a local and a global variable
- What is a reserved variable
- How to create, update and delete shell variables
- What are the roles of the following reserved variables:
HOME
,PATH
,PS1
- What are special parameters
- What is the special parameter
$??
- What is expansion and how to use them
- What is the difference between single and double quotes and how to use them properly
- How to do command substitution with
$()
and backticks
- How to perform arithmetic operations with the shell
- How to create an alias
- How to list aliases
- How to temporarily disable an alias
- How to execute commands from a file in the current shell
Read your /etc/profile
, /etc/inputrc
and ~/.bashrc
files.
Look at some files in the /etc/profile.d
directory.
Note: You do not have to learn about awk
, tar
, bzip2
, date
, scp
, ulimit
, umask
, or shell scripting, yet.
- <o> : A script that creates an alias.
- Name of alias:
ls
- Value:
rm *
- Name of alias:
- Hello you : A script that prints
hello user
, where user is the current Linux user. - The path to success is to take massive, determined action : A script that adds
/action
to thePATH
./action
should be the last directory the shell looks into when looking for a program. - If the path be beautiful, let us not ask where it leads : A script that counts the number of directories in the
PATH
. - Global variables : A script that prints all the enviroment variables.
- Local variables : A script that lists all local variables and enviroment variables, and functions.
- Name of variable :
HOLBERTON
- Value :
Betty
- Name of variable :
- Local variable : A script that creates a new local variable.
- Global variable : A script that creates a new global variable.
- Name of variable :
HOLBERTON
- Value :
Betty
- Name of variable :
- Every addition to true knowledge is an addition to human power : A script that prints the results of the addition of 128 with the value stored in the enviroment variable
TRUEKNOWLEDGE
, followed by a new line.- Remember to export variable TRUEKNOWLEDGE :
export TRUEKNOWLEDGE=1209
- Run command this way:
./8-true_knowledge | cat -e
- Remember to export variable TRUEKNOWLEDGE :
- Divide and rule : A script that prints the result of
POWER
divide byDIVIDE
, followed by a new line.POWER
andDIVIDE
are environment variables.- Variables values;
- export POWER=42784
- export DIVIDE=32
- Run command this way:
./9-divide_and_rule | cat -e
- Love is anterior to life, posterior to death, initial of creation, and the exponent of breath : A script that displays the result of
BREATH
to the power ofLOVE
.BREATH
andLOVE
are enviroment variables.- The script should display the result, followed by a new line.
- There are 10 types of people in the world -- Those who understand binary, and those who don't : A script that converts a number from base 2 to base 10.
- The number in base 2 is stored in the enviroment variable
BINARY
. - The script should display the number in base 10, followed by a new line.
- The number in base 2 is stored in the enviroment variable
- Combination : A script that prints all possible combinations of two letters, except
oo
.- Letters are lower cases, from
a
toz
. - One combination per line.
- The output should be alpha ordered, starting with
aa
. - Do not print
oo
. - Your script file should contain maximum 64 characters.
- Letters are lower cases, from
- Floats : A script that prints a number with two decimal places, followed by a new line.
- The number will be stored in the enviroment variable
NUM
.
- The number will be stored in the enviroment variable
- Decimal to Hexadecimal : A script that converts a number from base 10 to base 16.
- The number is base 10 is stored in the enviroment variable
DECIMAL
. - The script should display the number in base 16, followed by a new line.
- The number is base 10 is stored in the enviroment variable
- Everyone is a proponent of strong encryption : A script that encodes and decodes text using the rot13 encryption. Assume ASCII.
- The eggs of the brood need to be an odd number : A script that prints every other line from the input, starting with the first line.
- I'm an instant star. Just add water and stir. : A script that adds the two numbers stored in the enviroment variables
WATER
andSTIR
and prints the results.WATER
is in basewater
.STIR
is in basestir
.- The result should be in base
behlnort
.