Lanaru
Lanaru

Reputation: 9711

Parsing command line arguments in bash - strange behavior

#!/bin/bash

for arg
do
        echo "$arg"
done

In file called compile.

I tried this:

compile ha -aa -bb -cc -dd -ee -ff -gg
ha
-aa
-bb
-cc
-dd

-ff
-gg

Why does -ee not show up? In fact, it seems that -[e]+ does not show up.

Upvotes: 0

Views: 281

Answers (2)

rici
rici

Reputation: 241681

Because the version of echo you use takes -e as an option (meaning "expand escape characters").

Edited to add:

The standard response to this type of question is "use printf", which is strictly accurate but somewhat unsatisfactory. printf is a lot more annoying to use because of the way it handles multiple arguments. Thus:

$ echo -e a b c
a b c
$ printf "%s\n" -e a b c
-e
a
b
c
$ printf "%s" -e a b c
-eabc$ # That's not what I wanted either

Of course, you just need to remember to quote the entire argument sequence, but that can lead to annoying quote-escaping issues.

Consequently, I offer for your echoing pleasure:

$ ech-o() { printf "%s\n" "$*"; }
$ ech-o -e a b c
-e a b c
$

Upvotes: 4

hellerpop
hellerpop

Reputation: 509

Here it says that it's not possible with GNU echo but you could use printf.

for arg
do
    printf "%s\n" "$arg"
done

Upvotes: 1

Related Questions