kayvortex
February 26th, 2010, 05:24 AM
I've built a simple bash shell script, but at some point I need to do a:
var1="$(echo "$var2" | sed fairly_complex_substitute_expr -)"
So, I need to make sure echo will actually echo var2 (which could be anything). However, since bash's echo (and also /bin/echo) does not recognize "--" as an end-of-options option, I'm pretty much stuck if var2 is "-e" or "-n" or "-E", right?
Does anybody know of a way to make bash's echo print out "-n", for example. Id est, does anyone know how to make the following work:
echo '-n' '-e' '-E' # does not echo "-n", "-e", or "-E"
I could, of course, use printf:
printf -- "-n\n"
which works OK; or put a character in and then cut it:
var="_$var" && echo "$var" | cut -c 2-
or use awk:
echo | awk '{ print substr("'"$var"'",1) }'
but this seems like a pointless complication when
echo -- -n
would have been so much simpler! (Zsh's echo allows me to use "-" to signify the end of options; although why it's not "--" I don't know.)
If I am genuinely stuck with the more convoluted methods, does anyone at least know why this behaviour is not implemented?
var1="$(echo "$var2" | sed fairly_complex_substitute_expr -)"
So, I need to make sure echo will actually echo var2 (which could be anything). However, since bash's echo (and also /bin/echo) does not recognize "--" as an end-of-options option, I'm pretty much stuck if var2 is "-e" or "-n" or "-E", right?
Does anybody know of a way to make bash's echo print out "-n", for example. Id est, does anyone know how to make the following work:
echo '-n' '-e' '-E' # does not echo "-n", "-e", or "-E"
I could, of course, use printf:
printf -- "-n\n"
which works OK; or put a character in and then cut it:
var="_$var" && echo "$var" | cut -c 2-
or use awk:
echo | awk '{ print substr("'"$var"'",1) }'
but this seems like a pointless complication when
echo -- -n
would have been so much simpler! (Zsh's echo allows me to use "-" to signify the end of options; although why it's not "--" I don't know.)
If I am genuinely stuck with the more convoluted methods, does anyone at least know why this behaviour is not implemented?