On Sun, Mar 06, 2016 at 05:32:16AM +0100, Martijn Dekker wrote:
> The command 'set -o' shows the current shell options in an unspecified
> format. Less well-known is the variant 'set +o', which should output the
> current shell options "in a format that is suitable for reinput to the
> shell as commands that achieve the same options settings".[*]
> 
> That means it should be possible to do something like
> 
>         save_options=$(set +o)
> 
> then change some options, then later restore the shell options with
> 
>         eval "$save_options"
> 
> On all pdksh variants (as well as zsh), 'set +o' is currently inadequate
> for that purpose because it only outputs the currently active shell
> options, and not the inactive ones.
> 
> Even the old ksh88, which pdksh is a clone of (and on which most of
> POSIX is based), acts correctly in this regard.
> 
> This simple patch makes 'set +o' compatible with the POSIX spec.

Makes sense to me. However, I'm somewhat concerned this might break
existing shell scripts. Anyone here have a script that would break with
this change? Or a script that relies on POSIX behaviour and gets fixed
with this diff?

natano


> 
> Thanks,
> 
> - M.
> 
> [*]
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_25_03
>     (scroll down to '+o')
> 
> 
> Index: bin/ksh/misc.c
> ===================================================================
> RCS file: /cvs/src/bin/ksh/misc.c,v
> retrieving revision 1.53
> diff -u -p -u -r1.53 misc.c
> --- bin/ksh/misc.c    21 Dec 2015 04:57:50 -0000      1.53
> +++ bin/ksh/misc.c    6 Mar 2016 04:27:20 -0000
> @@ -240,8 +240,10 @@ printoptions(int verbose)
>               /* short version ala ksh93 */
>               shprintf("set");
>               for (i = 0; i < NELEM(options); i++)
> -                     if (Flag(i) && options[i].name)
> -                             shprintf(" -o %s", options[i].name);
> +                     if (options[i].name)
> +                             shprintf(" %co %s",
> +                                      Flag(i) ? '-' : '+',
> +                                      options[i].name);
>               shprintf("\n");
>       }
>  }
> Index: bin/ksh/ksh.1
> ===================================================================
> RCS file: /cvs/src/bin/ksh/ksh.1,v
> retrieving revision 1.175
> diff -u -p -u -r1.175 ksh.1
> --- bin/ksh/ksh.1     4 Mar 2016 18:16:50 -0000       1.175
> +++ bin/ksh/ksh.1     6 Mar 2016 04:27:20 -0000
> @@ -3734,7 +3734,8 @@ options (with single letter names) can b
>  .Ic set Fl o
>  with no option name will list all the options and whether each is on or off;
>  .Ic set +o
> -will print the long names of all options that are currently on.
> +will print the current shell options in the form of a command that
> +can be reinput to the shell to achieve the same options settings.
>  .Pp
>  Remaining arguments, if any, are positional parameters and are assigned, in
>  order, to the positional parameters (i.e. $1, $2, etc.).

Reply via email to