From owner-freebsd-questions@FreeBSD.ORG Wed Apr 9 20:22:43 2014 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id CA738E93 for ; Wed, 9 Apr 2014 20:22:43 +0000 (UTC) Received: from mx01.qsc.de (mx01.qsc.de [213.148.129.14]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 66F191AC5 for ; Wed, 9 Apr 2014 20:22:43 +0000 (UTC) Received: from r56.edvax.de (port-92-195-84-247.dynamic.qsc.de [92.195.84.247]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mx01.qsc.de (Postfix) with ESMTPS id F00E23DA57; Wed, 9 Apr 2014 22:22:34 +0200 (CEST) Received: from r56.edvax.de (localhost [127.0.0.1]) by r56.edvax.de (8.14.5/8.14.5) with SMTP id s39KLSbq002854; Wed, 9 Apr 2014 22:21:28 +0200 (CEST) (envelope-from freebsd@edvax.de) Date: Wed, 9 Apr 2014 22:21:28 +0200 From: Polytropon To: Gary Aitken Subject: Re: blindly passing on shell flags ($- )? Message-Id: <20140409222128.9a323764.freebsd@edvax.de> In-Reply-To: <53459F47.6080407@dreamchaser.org> References: <53459F47.6080407@dreamchaser.org> Organization: EDVAX X-Mailer: Sylpheed 3.1.1 (GTK+ 2.24.5; i386-portbld-freebsd8.2) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Cc: FreeBSD Mailing List X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list Reply-To: Polytropon List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Apr 2014 20:22:43 -0000 On Wed, 09 Apr 2014 13:28:07 -0600, Gary Aitken wrote: > 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. No. Those "flags" (command line arguments or parameters) are represented in $* and $@. > 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 $-. The getopts mechanism is for command line parameters, usually in the form -[a-z0-9]. > 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. You just need to call the new script with $@ and append (or prepend) your additional parameters. > The flags seem to be treated as a normal arg. Flags _are_ normal args (when we agree that "flag" means a command line argument, usually called an option, that starts with a - and in most cases is just one letter). :-) > 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. True, that doesn't sound like a good solution. > What am I missing? The exact wording of the manual. :-) Allow me to quote from "man sh": $- (hyphen) Expands to the current option flags (the single-letter option names concatenated into a string) as specified on invoca- tion, by the set built-in command, or implicitly by the shell. This does _not_ refer to positional parameters (command line parameters) which are usually represented in $* and $@ (with the difference that $@ honors "multi word arguments" being _one_ argument, whereas $* does not do so, as explained in the manual as well). Short demonstration: % ./sh-opt-test.sh "foo bar" -a baz $@ is: foo bar -a baz $* is: foo bar -a baz $- is: P $# is: 3 $0 is: ./sh-opt-test.sh $1 is: "foo bar" $2 is: "-a" $3 is: "baz" $4 is: "" % bash sh-opt-test.sh "foo bar" -a baz $@ is: foo bar -a baz $* is: foo bar -a baz $- is: hBP $# is: 3 $0 is: sh-opt-test.sh $1 is: "foo bar" $2 is: "-a" $3 is: "baz" $4 is: "" As you can see, I have manually "set -P" in sh-opt-test.sh which is reflected as "P" in the $- variable. Of course it doesn't have any effect here. When called via bash, you can see that bash has added the flags h and B. % cat sh-opt-test.sh #!/bin/sh set -P echo '$@ is:' $@ echo '$* is:' $* echo '$- is:' $- echo '$# is:' $# echo '$0 is:' $0 echo '$1 is: "'$1'"' echo '$2 is: "'$2'"' echo '$3 is: "'$3'"' echo '$4 is: "'$4'"' exit 0 -- Polytropon Magdeburg, Germany Happy FreeBSD user since 4.0 Andra moi ennepe, Mousa, ...