Search This Blog

Monday, July 8, 2019

Special shell parameters

The shell treats several parameters specially.
These parameters may only be referenced; assignment to them is not allowed.

1 - $?
It expands to the exit status of the most recently executed foreground *pipeline.
0 exit status means the command was successful without any errors.
non-zero (1-255 values) exit status means command was failure.

*Pipeline: In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (stdout) is passed directly as input (stdin) to the next one.

2 - $_
The underscore variable is set at shell startup and contains the absolute file name of the shell or script being executed as passed in the argument list.
i.e: if you create a shell script with just echo $_ it will spit out the path of that script.

Subsequently, it expands to the last argument to the previous command.
e.g:
$ mkdir /home/xbot/foo/bar/dir
$ cd $_
$ echo $?
0
$ pwd
/home/xbot/foo/dir