blindly passing on shell flags ($- )?

dteske at dteske at
Wed Apr 9 19:41:44 UTC 2014

> -----Original Message-----
> From: Gary Aitken [mailto:garya at]
> Sent: Wednesday, April 9, 2014 12:28 PM
> To: FreeBSD Mailing List
> Subject: blindly passing on shell flags ($- )?
> As I read the man page for sh and bash, the variable $- is supposed to
> represent the flags passed to the script.  However, whenever I look at it
it is
> empty.

$- represents the flags set with the "set" command, not positional
For positional arguments, see either "$@" or "$*".

> Reading between the lines, it appears one may have to call getopts so the
> shell knows what the flags are, but that doesn't seem to change what's
> available in $-.

getopts operates on positional arguments ($@ or $*) not option-flags.
Perhaps confusing is that the "set" command is used in modification of
either option-flags (primary function) or positional arguments (arguably
secondary function).

For example, changing an option-flag (which would change value of $-)

	set -e

And example for changing positional arguments (post-invocation; having
an effect on the $@ or $* variables)

	set -- arg1 arg2 arg3 ...

> Fundamentally, I want to add an arg (not a flag) and pass the whole
> on to another script.  Seems like this should be trivial but I can't get
it to work.

You would use "$@" in passing positional arguments to another script (this
will ensure proper expansion so each positional argument is passed as-is;
unlike "$*" which will join them together separated by the internal field
aka $IFS).

> The flags seem to be treated as a normal arg.
> The only way I seem to be able to get what I want is to interpret all the
> in the first script and pass them explicitly to the second, which is what
> trying to avoid.
> foo:
>   #!/bin/sh
>   echo foo here
>   echo flags $-
>   echo args $1 $2
>   bar $- INSERTED $1 $2
>   getopts ah flag
>   bar $- INSERTED $1 $2

> bar:
>   #!/bin/sh
>   echo bar here
>   echo flags $-
>   echo args $1 $2 $3
> $foo -a abc
> foo here
> flags
> args -a abc
> bar here
> flags
> args INSERTED -a abc
> bar here
> flags
> args INSERTED -a abc
> What am I missing?

Use "$@" instead of "$-" -- flags are not args.

The information contained in this message is proprietary and/or confidential. If you are not the intended recipient, please: (i) delete the message and all copies; (ii) do not disclose, distribute or use the message in any manner; and (iii) notify the sender immediately. In addition, please be aware that any message addressed to our domain is subject to archiving and review by persons other than the intended recipient. Thank you.

More information about the freebsd-questions mailing list