About Special Parameters
- Special parameters are set by the shell to store information about aspects of its current state
- Example 1: the number of arguments of the current script
- Example 2: the exit code of the last command.
- Their names are nonalphanumeric characters (for example, *, #, and _). Variables are identified by a name.
We'll here explain and demonstrate a few special parameters
The * special parameter
- ($*) Expands to the positional parameters, starting from one
1$
2$ cat show
3#!/bin/bash
4#
5
6echo $*
7$
8$
9$ ./show one two three
10one two three
11$
The # special parameter
- ($#) Expands to the number of positional parameters in decimal.
1$
2$ cat count
3#!/bin/bash
4#
5
6echo $#
7$
8$ ./count one two three
93
10$
The ? special parameter
- ($?) Expands to the exit status of the most recently executed foreground pipeline (or command)
1$
2$ ls non-existant-file-dir
3ls: cannot access 'non-existant-file-dir': No such file or directory
4$
5$ echo $?
62
7$
8$ ls
9count show ten three
10$
11$ echo $?
120
13$
The $ special parameter
- ($$) Expands to the process ID of the shell. In a subshell, it expands to the process ID of the invoking shell, not the subshell.
1$ cat showpid
2#!/bin/bash
3#
4
5echo $$
6$
7$ source showpid
845128
9$ bash showpid
1053132
11$
12$ ps
13 PID TTY TIME CMD
14 45128 pts/0 00:00:00 bash
15 53586 pts/0 00:00:00 ps
16$