Command Output in Shell Scripts
The last of the four scripting objectives, and the one that turns a script from a list of commands into something that reacts. Command substitution, arithmetic on captured values, branching on exit status and reading output into an array are all run here.
Shell Scripts Guide 65 of 67 Intermediate
- OSRHEL 10.0
- Kernel6.12.0-55.9.1.el10_0
- dnf4.20.0
- Flatpak1.16.0
- TimeAbout 13 min
$(command) is preferred over backticks: it nests without escaping and is easier to read. Both work and the exam accepts either. Command substitution strips trailing newlines, which is usually what you want and occasionally is not.
| 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 the last of the four scripting objectives. This matters because capturing command output is what turns a list of commands into a script that decides something for itself.
- capturing output, counting it, and branching on the result
- comparing the two substitution syntaxes, and what each of them strips
- putting all four scripting objectives into a single script
Before you start
- A shell.
- The session writes and removes
~/scripts.
-
Capturing, counting and branching
-
Two syntaxes, and what gets stripped
-
All four objectives in one script