2026-01-13 19:27 UTC
@awb You will well know that there are many "tricks" to using shells, but I think the biggest challenge for many is trying to set parent variables in subshells and wondering why the parent shell doesn't see the change.
This brings up the subtle difference between a () compound command and a {} compound command/function.
This treatment of variables can also be used to advantage if you're on the command line and want to mess with a bunch of env variables to test something then reset them back to their original values. How? Invoke a new shell for the duration of the test sequence:
$ PS1='sub> ' sh
sub> Mess with env variables
sub> run test
sub> exit
$ # original shell vars untouched
The other technique that is useful but perhaps not well know is using "exec" to rearrange stdin/stdout/stderr. For example, contrast this broken fragment:
lno=0
while read line &1
do stuff
to ensure all future stderr is redirected to stdout without needing to explicitly state it in subsequent commands.
Replies (0)
No replies.