can not use 'e' as first (or only) argument to getopt

With the following (simplified) code:

#!/bin/bash

args=`getopt -q -u -o efg -l exit,follow,go -- "$@"`
set -- $args
echo $args

‘e’, ‘f’ and ‘g’ being the short options, ‘exit’, ‘follow’, ‘go’ the long options, and ‘param’ some parameter, I can not use ‘e’ as first - or only - option.
Let’s call the code above ‘test’.


$ test -e -f -g   param
-f -g -- param

$ test -efg   param
-f -g -- param

but


$ test -f -e -g   param
-f -e -g -- param

$ test -feg   param
-f -e -g -- param

I hope I’m missing something … otherwise it doesn’t make sense again. The long form ‘–exit’ works.

OK. I could look in getopt source and maybe find something weird. But I’m not trying to debug getopt. I just want to use "-e’ for ending a process in a script (other letters such as “-k”, “-q” and “-x” are already used for other options).

please try again wrote:
> Let’s call the code above ‘test’.

Not possible. test is a shell built-in

Oh. It doesn’t matter. Please read:


$ pwd
/tmp
$ ./test -e -f -g   param
-f -g -- param

or call it something else. That’s not the problem.

This is the line that makes you wonder:

echo $args

because it evaluates into

echo -e -f -g param

and -e is an option for echo. Read

man echo

Yesssss ! That’s it.

I can actually use this option, just not ‘echo’ it (and I don’t intend to). I should have used printf instead for testing.


args=`getopt -q -u -o efg -l exit,follow,go -- "$@"`
set -- $args
printf "%s
" "$args"


./test -e -f -g param
 -e -f -g -- param

:slight_smile:

Thanks a lot, Henk! You saved my day … or night. … or day… anyway.

You are welcome.

I normaly use ksh for scripts and then use print instead of echo. print has the same trap, but you can avoid it by using* - *or as an end of options signal. echo does not seem to have such a feature.


henk@boven:~> exec ksh
henk@boven:/home/henk> print -a "lalala"
ksh: print: -a: unknown option
Usage: print -enprsvC] -f format] -u fd] [string ...]
henk@boven:/home/henk> print - -a "lalala"
-a lalala
henk@boven:/home/henk>

And as you see, print does object against unknown options, while echo just prints them as one of the strings:

henk@boven:~> echo -a "lalala"
-a lalala
henk@boven:~> echo -a -e "lalala"
-a -e lalala
henk@boven:~> echo -e -a "lalala"
-a lalala
henk@boven:~>

And the POSIX implementation of echo says:

The echo utility shall not recognize the “–” argument in the manner specified by Guideline 10 of the Base Definitions volume of IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines; “–” shall be recognized as a string operand.

Implementations shall not support any options.

Better, because your case is covered. but then you must use the POSIX one and not the default one.

Well you can use echo, just quote the variable to echo,
e.g. echo “$args”

On Thu, 19 Jul 2012, please try again wrote:

>
> Yesssss ! That’s it.
>
> I can actually use this option, just not ‘echo’ it (and I don’t intend
> to). I should have used printf instead for testing.
>
>
> Code:
> --------------------
>
> args=getopt -q -u -o efg -l exit,follow,go -- "$@"
> set – $args
> printf "%s
" “$args”
>
> --------------------
>
>
>
> Code:
> --------------------
>
> ./test -e -f -g param
> -e -f -g – param
>
> --------------------
>
>
> :slight_smile:
>
> Thanks a lot, Henk! You saved my day … or night. … or day… anyway.
>
>
> –
> please_try_again
> ------------------------------------------------------------------------
> please_try_again’s Profile: http://forums.opensuse.org/member.php?userid=10877
> View this thread: http://forums.opensuse.org/showthread.php?t=476835
>
>

Did you realy try this out?

Because it can’t be true. The quoting you show is a feature of the shell* (bash, ksh* and more of them). In this case there is nothing inside the quotes that is special to the shell. Thus the result after all the shell expansions are done is the same. And echo sees the same: a range of arguments where the firsts is* -e* and thus an option.

henk@boven:~> args="-e -a lalala"
henk@boven:~> echo $args
-a lalala
henk@boven:~> 

It was 6:00 AM. :shame:

But


$ echo "$args"
-e -a lalala

That’s what @Barry_Nichols meant.

I’m kind of confused with this stupid thread. I should just run less tests. I just wanted to pass all options (there were much more than in the example I posted) in order to discard conflicting options. I should have put $args in quote or use printf. But again, it was 6:00 AM, and I could not see anymore.

Ok, my fault :frowning: the shell makes it one argument instead of several.

But as soon as args contains* -e* only (or one of the other echo options) you are gone again.

henk@boven:~> args="-e"
henk@boven:~> echo "$args"

henk@boven:~>

echo is simply badly designed. The POSIX version tries to take out the stinge by simply forbidding any options.

Lack of sleep will one time destroy the world :frowning: