POSIX Shell reverse oblique confusion
I'm trying to create a wrapper script with simple functionality, but I can't seem to wrap my head around on how to properly handle backslashes. One of my functions looks like this:
#!/bin/sh
func() {
cmd="$*"
printf "%s\n" "$cmd"
echo -e $cmd
}
func '%{NAME}\\n'
This is the correct conclusion as I need it:
%{NAME}\\n %{NAME}\n
Now the problem is that I cannot use "echo -e" as this script must be run on a * nix system where the echo command does not have the "-e" flag (eg HPUX), this is also why I have to use sh rather than bash. Since I want to make this script as portable as possible, I would not use tr / sed languages or (even worse) script.
The input string format can be chosen arbitrarily. I used% {NAME} for a reason, because if it were just normal characters, something like this would work:
#!/bin/sh
func() {
cmd="$*"
printf "%s\n" "$cmd"
printf $cmd
echo
}
func 'NAME\\n'
Unfortunately this breaks with characters like "%":
%{NAME}\\n
func.sh: line 6: printf: `{': invalid format character
How can I get the output I want (hopefully) with just printf, which I think is the most portable function to use?
source to share