Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 09 Apr 2014 13:28:07 -0600
From:      Gary Aitken <garya@dreamchaser.org>
To:        FreeBSD Mailing List <freebsd-questions@freebsd.org>
Subject:   blindly passing on shell flags ($- )?
Message-ID:  <53459F47.6080407@dreamchaser.org>

next in thread | raw e-mail | index | archive | help
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.

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 $-.

Fundamentally, I want to add an arg (not a flag) and pass the whole
shebang on to another script.  Seems like this should be trivial but I
can't get it to work.  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 flags in the first script and pass them explicitly to the second,
which is what I'm 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?

Thanks,

Gary
  



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?53459F47.6080407>