KandZ – Tuts

We like to help…!

Linux CLI 54 🐧 shell script variables

a - variables
variables are used to store and manipulate data
Declaring variables → by assigning a value to them using =
There should be no spaces around =
my_variable="Hello, World!" → declares a variable
Accessing variables → using $ with the variable name you can access its value
echo $my_variable → prints the data stored in the variable
You can use variables in shell scripts and in the Command line.
You can unset a variable using unset

b - special variables
Shell scripts have several special variables that are automatically set by the shell
$0 → The name of the script.
$1 to $9 → Positional parameters passed to the script.
$# → Number of positional parameters.
$* → All the positional parameters as a single word.
$@ → All the positional parameters as separate words.
$$ → Process ID of the current script.
$? → Exit status of the last command executed.

Leave a Reply