I am using echo in bash. Have created a function prargv which takes a number of arguments.
Example:
prargv "-e" "--examples"
Inside prargv, I want to print all the arguments using echo
echo "$@"
This returns
--examples
rather than
-e --examples"
This problem can be fixed by introducing a blank space
echo " $@"
How can I avoid echo interpreting -e when I do not put a blank at the beginning?
drl
2
Hi.
As seen in man echo, for some systems -e is treated as an option:
-e enable interpretation of backslash escapes
Using printf as in:
printf "%s\n" "-e --example"
produces:
-e --example
This is for:
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian 5.0.8 (lenny, workstation)
printf - is a shell builtin [bash]
This also works with:
/usr/bin/printf "%s\n" "-e --example"
Best wishes ... cheers, drl
That is correct, the -e is treated as an option. I found it strange that echo is interpreting it as I put $@ in double quotes.
drl
5
Hi.
An illustration of $* and $@:
#!/usr/bin/env bash
echo
echo " asterisk"
echo "$*"
echo
echo " at sign"
echo "$@"
echo
echo " asterisk"
printf "%s\n" "$*"
echo
echo " at sign"
printf "%s\n" "$@"
producing:
$ ./s1 -e --example
asterisk
-e --example
at sign
--example
asterisk
-e --example
at sign
-e
--example
See man pages for details, and experiment.
Best wishes ... cheers, drl