0
#!/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.

Lanaru
  • 9,421
  • 7
  • 38
  • 64

2 Answers2

4

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
$
rici
  • 234,347
  • 28
  • 237
  • 341
1

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

for arg
do
    printf "%s\n" "$arg"
done
Community
  • 1
  • 1
hellerpop
  • 509
  • 4
  • 7