sh - POSIX Shell backslash confusion -
i trying create shell script simple functionality, seems can not wrap head how handle backslashes correctly. 1 of functions this:
#!/bin/sh func() { cmd="$*" printf "%s\n" "$cmd" echo -e $cmd } func '%{name}\\n' this correct output, need it:
%{name}\\n %{name}\n now problem is, can not use "echo -e", script needs run on *nix system, echo command not have "-e" flag (hpux instance), why have use sh , not bash. want make script portable possible, i'd stay clear of using tr/sed or (even worse) script languages.
the format of input string can chosen arbitrarily. using %{name} of reason, because if regular chars, work:
#!/bin/sh func() { cmd="$*" printf "%s\n" "$cmd" printf $cmd echo } func 'name\\n' unfortunately breaks characters such "%":
%{name}\\n func.sh: line 6: printf: `{': invalid format character how can desired result, (hopefully) using printf, in understanding portable function use?
you can use %b in printf:
func() { cmd="$@"; printf "%s\n%b" "$cmd" "$cmd"; } then call as:
func '%{name}\n\n' this print:
%{name}\n\n %{name}
Comments
Post a Comment