I had to move all my script from AIX to RH8.5 and i'm having issues with the escape charater.
Indeed the "\" is not working anymore in my "echo" line.
I can replace all the "echo" to "echo -e" but i'm looking for a better solution without having to touch to all the scripts.
Hi,
If you're using Bash as the default shell / interpreter of all your scripts, you might want to read about shopt built-in command and xpg_echo shell option.
(to enable backslash sequences interpretation by default, use e.g. shopt -s xpg_echo)
i'm using ksh.. should have said it sry and your solution actualy work in bash, i tried it but in my case i can't find anything similar..
my version of ksh is : sh (AT&T Research) 93u+ 2012-08-01
Unfortunately I don't know much about ksh internals, but some solutions found online suggest making sure that /usr/xpg4/bin is added at the beginning of your PATH (visible in the script, assuming /usr/xpg4/bin/echo exists and PATH modification will trigger internal ksh mechanism for interpreting all \ sequences, as if the xpg_echo was enabled)
Whichever one is "expandable" directly in your ksh instance. PATH variable is (usually, by default) exported, so its value in your current shell will be inherited by any subshell called from it - this should work fine, even if your current shell is of different type, than a subshell called afterwards (see bash vs ksh in the example below), unless it's overwritten by a value from specific startup file (during specific type of invocation).
PATH value is most commonly set inside shell's startup scripts (either locally e.g. ~/.kshrc, ~/.bashrc, ~/.zshrc ... or globally e.g. /etc/environment ...)
But as it turns out, modifying PATH variable can trigger certain ksh behavior, even if /usr/xpg4/bin does not contain the echo binary, or more so - even if it doesn't exist at all in the filesystem hierarchy, see below:
$ echo "$0"
/bin/bash
$ printenv PATH
/home/theia/.rbenv/shims:/home/theia/.rbenv/bin:/home/theia/dsdriver/adm:/home/theia/dsdriver/bin:/usr/local/cargo/bin:/usr/local/go/bin:/usr/local/go-packages/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go-packages/bin:/usr/local/apache-maven-3.9.5/bin:/home/theia/.local/bin:/home/theia/.dotnet/tools
$ which ksh
/usr/bin/ksh
$ /usr/bin/ksh --version
version sh (AT&T Research) 93u+m/1.0.0-beta.2 2021-12-17
$ /usr/bin/ksh
$ echo "$0"
/usr/bin/ksh
$ printenv PATH
/home/theia/.rbenv/shims:/home/theia/.rbenv/bin:/home/theia/dsdriver/adm:/home/theia/dsdriver/bin:/usr/local/cargo/bin:/usr/local/go/bin:/usr/local/go-packages/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go-packages/bin:/usr/local/apache-maven-3.9.5/bin:/home/theia/.local/bin:/home/theia/.dotnet/tools
$ ls -l
total 4
-rwxr-xr-x 1 theia users 47 Jan 17 08:42 echotest.ksh
$ cat echotest.ksh
#!/usr/bin/ksh
echo "yet\n\tanother\n\t\tline"
$ ./echotest.ksh
yet\n\tanother\n\t\tline
$ export PATH="/usr/xpg4/bin:$PATH"
$ ./echotest.ksh
yet
another
line
$ ls -ld /usr/xpg4/bin
ls: cannot access '/usr/xpg4/bin': No such file or directory
/usr/xpg4/bin/ exists in the Solaris OS only.
All shells prefer the builtin echo to the one that is found by PATH.
If a "blind" /usr/xpg4/bin in the PATH works in ksh on your OS then it's the most easy solution.
Another idea:
Try to overload the builtin echo with an echo function:
echo(){ builtin echo -e "$@"; }
Or make it even portable with printf; the following function should work in all standard-compliant shells on all OS:
echo()(IFS=" "; printf "%b\n" "$*")
Put that at the beginning of a shell script (after the shebang of course) , and its behavior should be the SysV style (like AIX, Solaris, HP-UX).
I tried it and it work 100% as i wanted, no need to change anything else but the PATH this is realy good.
I have 2 more questions about this xpg4 thing :
Is it best practice ? so i'm sure i can use it x)
Do you have more info about it because i don't really understand since i can't find any trace of xpg4 on my OS? (if you have any link that i can check !)