echo in ksh sh & bash

Hello,

I have lib file which contain a function that get text to print on screen by echo command.

Several scripts are inculde this lib and use this function.
Each one of them is written in different shell language (sh ksh & bash).
This causing some issues when using backslash charater as part of the test that sends to this function.
I would like to create two echo command which will executed according to the current shell.
How can I extarct the current shell which used by the calling script to the print function ?

Thanks a head
Alalush

echo is usually a shell builtin - otherwise it is /usr/bin/echo.

I am not understanding what you want to do - override the echo builtin?

Each shell command reads an environmental file before execution.

For sh, it's .profile
For ksh, it's the $ENV file.
For bash, it's the $BASH_ENV file.

For sh, the echo command is already /usr/bin/echo.

For ksh, you'll have to alias this command like so:

alias -x echo=/usr/bin/echo

or make it a function, I've seen aliases non-exported in some Solaris OS...

function echo
{
/usr/bin/echo $*
}

The same thing should work for bash.

To answer the question: "${0}" should be the CURRENT shell name. You could try to get the version information as well (might be limited and I haven't tried that but you should be able to get this from the environment variables).
The calling process is your parent. You should never check your parent's property. If you need - then most probably the application needs an re-design.
My guess is that the parent process should call this code with a pre-formatted text like "{bold}this{red}is{normal}some text". Of course we should ask ourselves whether it is a good idea to use shell scripts here (probably you should use python/perl/some high level language).

To correct your approach: If you call a script then its executable should be specified in the first line (ex. #!/bin/ksh). If you have several scripts and every is written for a different shell - you should execute them and this would execute a new shell/whatever instance. Since every shell seems to be a separate entity (as it seems from your description) - you should either normalize them (re-write everything to the same script/shell/whatever) or just leave them as it is right now without using any common shell script file. There is nothing worse than using thousands of different languages in a single project maintained by thousands of developers.
If your script is called from other scripts (using several different shells to execute them) - then I cannot understand how this is a library script. Maybe this is a script that is executed with some options specified?

By the way: My English is not the the best but bellow you can find somewhat corrected version of your post (maybe someone else can correct it more? or even correct my post?). Sorry for being the fussy kind:

Alalush ,

  • using escape sequences (eg. "\t") on shell strings works uniformly among most shells in the unix world (sh - the bourne shell , and ksh - korn shell) ;

  • however, this is not true for linux and bash (bourne again shell) ;

-- in bash, you must use the "-e" flag on echo , in order to produce the same effect for the escape sequences ;

hope this helps.

good luck , and success !

botao