Hands-on Lab·Red Hat Certified System Administrator
Shell Script Arguments
One objective, $1 and $2 by name. The parts worth practising are the argument count check that makes a script usable, and the difference between "$@" and $*, which is invisible until an argument contains a space.
Shell Scripts Guide 64 of 67 Beginner
- OSRHEL 10.0
- Kernel6.12.0-55.9.1.el10_0
- dnf4.20.0
- Flatpak1.16.0
- TimeAbout 11 min
"$@" expands to one word per argument, preserving spaces inside each. $* joins them into a single string and then word-splits it. "$@" with the quotes is almost always what you want, and the quotes are not optional.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RHCSA-A01 | 192.168.0.31 | RHEL 10.0 | Practice node (graded) - spare /dev/sda | 2 Core | 4 GB | 50 GB + 15 GB |
This guide includes
Use this for one objective that names $1 and $2 directly. This matters because refusing to run without arguments is part of the job - and "$@" and $* differ in a way that only shows up on multi-word arguments.
- reading the positional parameters, including
$0 - refusing to run without them, and exiting with a meaningful status
- separating
"$@"from$*, which differ once an argument has a space in it
Before you start
- A shell.
- The session writes and removes
~/scripts.
-
The parameters
-
Refusing to run without them
-
`"$@"` and `$*`