46.10 A Portable echo Command
One of the frustrating changes to UNIX (for me, at least) is the
newer versions of
echo
(8.6
)
that interpret escape sequences like
echo -n "Answer y for yes or n for no: " but the newer echo needs: echo "Answer y for yes or n for no: \c" and giving the wrong command to the wrong echo makes messy output. I've seen workarounds by Bruce Barnett and Liam R. E. Quin. I've turned them into this version. It sets shell variables that you use this way: $echo "Answer y for yes or n for no: ${nnl}" Can your shell script be set up for a particular UNIX version ahead of time? If it can, write your no - newline echo commands like the example above - and put the lines below at the top of your script: # UN-COMMENT THE LINE FOR YOUR SYSTEM: echo="echo -n" nnl= ;; # BSD #echo="echo" nnl="\c" ;; # Sys V #echo="echo -n" nnl= PATH=/usr/bin:$PATH; export PATH # SunOS Lines similar to those let the person who installs the script set the right echo version. But if your script is shared between many UNIX systems (across a networked filesystem) or runs on a system where users can choose BSD or System V features (like SunOS), your script will need to configure echo each time it runs. To do that, put the following code at the top of your script: case "`echo 'x\c'`" in 'x\c') echo="echo -n" nnl= ;; # BSD x) echo="echo" nnl="\c" ;; # Sys V *) echo "$0 quitting: Can't set up echo." 1>&2; exit 1 ;; esac In that code, the shell runs the current echo
command
and tests its output.
Newer echo
commands will interpret the You can handle other escape sequences and unprintable
characters in the same way.
For example, to make the code set
If you use bash , you can tell its echo command which way to act. The echo -e command always does backslash-escape interpretation. The echo -E command never interprets backslashes. And the option -n (no newline) works in both cases. - |
|