Pages

shell tip: echo * and printf

~$ mkdir testdir
~$ cd testdir
~/testdir$ touch -- -e \\141
~/testdir$ ls
-e \\141
~/testdir$ echo *
a
~/testdir$
note i used '--' with touch so '-e' would not be used as an option.
So what happened here?
-e was passed to echo as an option.
echo *
becomes
echo '-e' '\141'
which interpreted correctly the \141 escape sequence as 'a'
If you're going to use * be careful. Avoid echo try printf.
In this case:
~/testdir$ printf "%s\n" *
-e
\141
~/testdir$

No comments: