Two Special parameters in Linux shell script
This writing discusses two special parameters provided by Unix/Linux Bash shell. They are $* and $@. They are used when command line arguments are passed to shell scripts. The example scripts given in this article has been executed on RHEL9. Though the scripts may successfully run on many other flavours of Linux .
You may have seen two different forms of these two parameters, quoted version as well their unquoted version. They are used to store all the supplied arguments in the command line. But which of the below forms to use when?
$*
$@
“$*”
“$@”
$* and $@ takes each word supplied in the command line as a separate argument. Lets understand this by looking at example script given in figure 1.
Above script demonstrates the implications with unquoted form of $@ and $* . Lets view the run of the given script in our next figure.
Figure 2 includes multiple runs of our example script with different command lien argumnets each time. In the first run, two arguments linux and unix are passed. Both the parameters $* and $@ reports it correctly. But in our second run where only one arguments is passed, both of them do not work properly. We passed one multi-word argument. But both the parameters do not treat them as one argument. They take each word as a separate argument, and thus our second run provides incorrect values for these parameters. In the the third run , two arguments are passed. But both the parameters take each word provided in the command line as a separate argument , as said before.
Thus , we can say that $@ and $* are not reliable. Lets view their quoted form. To understand the same, we will use the same script with little modification as shown in figure 3.
Above script is showing the implications of “$*” ad “$@”. Figure 4 is demonstrating the execution of the modified script using different command line arguments.
Lets view the first run of the modified script. At the first run, we passed two argumnents. “$*” takes the complete command line arguments as a string and thus prints the whole string. Unlike “$*”, “$@” takes two arguments separately , and prints the correct result. On the second run, we don’t see any difference between their execution, as they produce same result. But we can not rely on “$*” as we have seen on the first run. Third run also concludes the same. On third run, we passed two arguments. And here “$@” prints the correct result.
Thus, we can conclude that except “$@” , all the other forms may provide the wrong information in one time or other. So, one should always prefer “$@” over $@, $* and “$*”.
Hoping this article gave you the clear picture of these two special parameters in Unix/Linux script. For more information on passing command-line arguments to shell script, you can refer to the book titled “RedHat Enterprise Linux 9 for Beginners” co-authored by me and published by BpB which is available on the below links:
and
Learn Linux with Geetanjali Mehra , an experienced IT professional and co-author of a Linux-based book. For details, click on https://alltechmantra.wordpress.com/linux-basic-concepts-and-administration/