On Mon, Oct 15, 2012 at 8:08 PM, DJ Mills <danielmil...@gmail.com> wrote: > while read -r attr state; do > if [[ $shellopts = *:"$attr":* ]]; then > set -o "$attr" > else > set +o "$attr" > fi > done < <(set -o)
Erm, correction, that won't work for the first and last values in shellopts. You could add colons to $shellopts ( [[ :$shellopts: = *:"$attr":* ]] ), or use an array: #### inarray() { local n=$1 h shift for h; do [[ $n = "$h" ]] && return done return 1 } IFS=: read -ra opts <<<"$shellopts" while read -r attr state; do if inarray "$attr" "${opts[@]}"; then set -o "$attr" else set +o "$attr" fi done < <(set -o) #### Or with bash 4, a more efficient version using an associative array: #### declare -A setopts IFS=: read -ra opts <<<"$shellopts" for opt in "${opts[@]}"; do setopts[$opt]=1 done unset opts while read -r attr state; do if ((opts[$attr])); then set -o "$attr" else set +o "$attr" fi done < <(set -o) #### I know that the shell options are known beforehand, but it's always a good idea to use safe coding practices. These methods are safe for any values in the "$shellopts" string, whereas yours would break for spaces or regex characters. You should also make sure to quote all of your expansions. Would it be an option to create the needed environment in a subshell, where you can set whatever options you'd like and not worry about them persisting? By the way, if anyone has a more efficient way to create an associative array like that other than that loop, I'd love to see it.