Bash Special Variables (Special Parameters)

As I sometimes do, I was searching for something that I always used but didn't know what it really did.
This time is the turn of bash Special Parameters. If you are able to use this in a good way it's like you have the power of the universe in your hand!!!!

Before giving you a simple list with examples, I need to specify another argument. Do you know what Positional Parameters are?

Positional Parameters
For the ones (like me) that want just a glance to understand what I'm talking about, here's what positional parameters are:
$1, $2, $3 etc..
Understood? 
Well, from bash(1) man (bash(1) man), the definition is:
positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell's arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameters may not be assigned to with assignment statements. The positional parameters are temporarily replaced when a shell function is executed (see FUNCTIONS below).
When a positional parameter consisting of more than a single digit is expanded, it must be enclosed in braces (see EXPANSION below).
In other words, when you call a command in bash and you put some text after the command itself, the program retrieves this text using $1, $2 etc, starting from one

The following will be the command for all the subsequent examples:
rjdio@rockserver:/home/rjdio# ./test.sh metal heavy 
A very simple example code for this example:
#!/bin/bash
echo $2 
echo $1
heavy is $2, metal is $1, so output is:
heavy
metal
I think I made it clear enough ;

Special Parameters
These listed below are instead special parameters. Each one has a special function, and for this reason you cannot assign a new value to them.
#!/bin/bash
echo "$*"
Output:
metal heavy
#!/bin/bash
for music in "$@"; do
    echo "great $music"
done
 Output:
great metal great heavy
#!/bin/bash
echo "$#"
Output:
2
#!/bin/bash
mkdir /home/rjdio/metal
if [ $? -eq 0 ]; then
    echo "/home/rjdio/metal created"
Output (suppose it all went well, the exit code in case the directory is successfully created is 0):
/home/rjdio/metal created


echo "$-"
Output (in most of my environments):
himBH
To find out the meaning of those command line options, simply type help set. In this case, these options mostly enables bash history. 


echo "$$"
Output (it's an ID, it's different from machine to machine - or between users even)
73471


echo "$!"
Output (it's an ID, remember)
12345


#!/bin/bash
echo "$2 $1 $0"
Output (remember, I'm launching this command line: ./test.sh metal heavy)
heavy metal ./test.sh


echo "$_$"
Output (if the last command I executed it's ./test.sh metal heavy)
heavy

Conclusions
Well, in this post I briefly described with examples all of the positional parameters. They are powerful and useful functions, that it's possible to use in your code in many ways (eg. when parsing command line parameters).
Another tip: in my opinion, to better understand how these option works, you should think of bash as a program. If it's a program, it can be launched and can have parameters, for example.

References
Man(1) Bash - simply the man page of BASH
Stackoverflow Special Parameters question - brief but complete list of all positional parameters and their functions.

Labels: