Skip to content

Latest commit

 

History

History

0x03-shell_variables_expansions

img

shell variable expansion

Intro

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

Resources

Read or watch.

  1. Expansions
  2. Shell arithmetic
  3. Variables
  4. Shell init files
  5. The alias command
  6. Technical writting

Man or Help

  • printenv
  • set
  • unset
  • export
  • alias
  • unalias
  • .
  • source
  • printf

Learning objectives

By the end of this project, you should be able to explain to anyone Without the help of google the following concepts

General

  • What happens when you type $ ls -l *.txt

Shell intialization files

  • What are the /etc/profile file and the /etc/profile.d directory
  • What is the ~/.bashrc file

Variables

  • 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 $??

Expansions

  • 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

Shell Arithmetic

  • How to perform arithmetic operations with the shell

The alias command

  • How to create an alias
  • How to list aliases
  • How to temporarily disable an alias

Other help pages

  • How to execute commands from a file in the current shell

More info

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.

Tasks

  1. <o> : A script that creates an alias.
    • Name of alias: ls
    • Value: rm *
  2. Hello you : A script that prints hello user, where user is the current Linux user.
  3. The path to success is to take massive, determined action : A script that adds /action to the PATH. /action should be the last directory the shell looks into when looking for a program.
  4. If the path be beautiful, let us not ask where it leads : A script that counts the number of directories in the PATH.
  5. Global variables : A script that prints all the enviroment variables.
  6. Local variables : A script that lists all local variables and enviroment variables, and functions.
    • Name of variable : HOLBERTON
    • Value : Betty
  7. Local variable : A script that creates a new local variable.
  8. Global variable : A script that creates a new global variable.
    • Name of variable : HOLBERTON
    • Value : Betty
  9. 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
  10. Divide and rule : A script that prints the result of POWER divide by DIVIDE, followed by a new line.
    • POWER and DIVIDE are environment variables.
    • Variables values;
    • export POWER=42784
    • export DIVIDE=32
    • Run command this way: ./9-divide_and_rule | cat -e
  11. 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 of LOVE.
    • BREATH and LOVE are enviroment variables.
    • The script should display the result, followed by a new line.
  12. 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.
  13. Combination : A script that prints all possible combinations of two letters, except oo.
    • Letters are lower cases, from a to z.
    • 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.
  14. 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.
  15. 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.
  16. Everyone is a proponent of strong encryption : A script that encodes and decodes text using the rot13 encryption. Assume ASCII.
  17. 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.
  18. I'm an instant star. Just add water and stir. : A script that adds the two numbers stored in the enviroment variables WATER and STIR and prints the results.
    • WATER is in base water.
    • STIR is in base stir.
    • The result should be in base behlnort.