On Mon, Oct 15, 2012 at 5:18 PM, Nikolai Kondrashov <nikolai.kondras...@redhat.com> wrote: > On 10/16/2012 12:08 AM, Greg Wooledge wrote: >> >> Sidestepping the direct question for a moment, you could use a temporary >> file instead of a command substitution to store the output. Then there >> would be no subshell involved. > > > Thanks, Greg. I don't like creating temporary files unnecessarily, so I went > the route of using SHELLOPTS value instead and massaging it into a set of > "set" calls on restore. Like this: > > declare -r shellopts="$1" > declare -r on_pattern="^(${shellopts//:/|})\$" > declare attr > > for attr in `set -o | awk '{print \$1}'`; do > if [[ $attr =~ $on_pattern ]]; then > set -o $attr > else > set +o $attr > fi > done > > Might need to do something with that awk invocation, though. > > Sincerely, > Nick >
Do not use for to iterate over the output of an unquoted command substitution. Instead, use a while read loop. See http://mywiki.wooledge.org/DontReadLinesWithFor and http://mywiki.wooledge.org/BashFAQ/001 I also don't understand the point of using a regex like that, seems to be way more complicated than it needs to be. A simple glob will suffice here. Assuming you're writing bash and not POSIX sh: while read -r attr state; do if [[ $shellopts = *:"$attr":* ]]; then set -o "$attr" else set +o "$attr" fi done < <(set -o)