10 # func_fatal_error message
11 # outputs to stderr a fatal error message, and terminates the program.
14 echo "test-echo.sh: *** $1" 1>&2
15 echo "test-echo.sh: *** Stop." 1>&2
19 # Ensure an 'echo' command that does not interpret backslashes.
21 # echo '\n' | wc -l prints 1 when OK, 2 when KO
22 # echo '\t' | grep t > /dev/null has return code 0 when OK, 1 when KO
23 # This problem is a weird heritage from SVR4. BSD got it right (except that
24 # BSD echo interprets '-n' as an option, which is also not desirable).
25 # Nowadays the problem occurs in 4 situations:
26 # - in bash, when the shell option xpg_echo is set (bash >= 2.04)
27 # or when it was built with --enable-usg-echo-default (bash >= 2.0)
28 # or when it was built with DEFAULT_ECHO_TO_USG (bash < 2.0),
29 # - in zsh, when sh-emulation is not set,
30 # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances,
31 # and HP-UX /bin/sh and IRIX /bin/sh behave similarly),
32 # - in Solaris /bin/sh and OSF/1 /bin/sh.
33 # We try the following workarounds:
34 # - for all: respawn using $CONFIG_SHELL if that is set and works.
35 # - for bash >= 2.04: unset the shell option xpg_echo.
36 # - for bash >= 2.0: define echo to a function that uses the printf built-in.
37 # - for bash < 2.0: define echo to a function that uses cat of a here document.
38 # - for zsh: turn sh-emulation on.
39 # - for ksh: alias echo to a function that uses cat of a here document.
40 # - for Solaris /bin/sh: respawn using /bin/ksh and rely on the ksh workaround.
41 # - otherwise: respawn using /bin/sh and rely on the workarounds.
42 # When respawning, we pass --no-reexec as first argument, so as to avoid
43 # turning this script into a fork bomb in unlucky situations.
45 if echo '\t' | grep t > /dev/null; then
46 have_echo=yes # Lucky!
48 # Try the workarounds.
49 # Respawn using $CONFIG_SHELL if that is set and works.
50 if test -z "$have_echo" \
51 && test "X$1" != "X--no-reexec" \
52 && test -n "$CONFIG_SHELL" \
53 && test -f "$CONFIG_SHELL" \
54 && $CONFIG_SHELL -c 'echo '\t' | grep t > /dev/null'; then
55 exec $CONFIG_SHELL "$0" --no-reexec "$@"
58 # For bash >= 2.04: unset the shell option xpg_echo.
59 if test -z "$have_echo" \
60 && test -n "$BASH_VERSION" \
61 && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then
65 # For bash >= 2.0: define echo to a function that uses the printf built-in.
66 # For bash < 2.0: define echo to a function that uses cat of a here document.
67 # (There is no win in using 'printf' over 'cat' if it is not a shell built-in.)
68 if test -z "$have_echo" \
69 && test -n "$BASH_VERSION"; then \
70 if type printf 2>/dev/null | grep / > /dev/null; then
71 # 'printf' is not a shell built-in.
79 # 'printf' is a shell built-in.
85 if echo '\t' | grep t > /dev/null; then
89 # For zsh: turn sh-emulation on.
90 if test -z "$have_echo" \
91 && test -n "$ZSH_VERSION" \
92 && (emulate sh) >/dev/null 2>&1; then
95 # For ksh: alias echo to a function that uses cat of a here document.
96 # The ksh manual page says:
97 # "Aliasing is performed when scripts are read, not while they are executed.
98 # Therefore, for an alias to take effect, the alias definition command has
99 # to be executed before the command which references the alias is read."
100 # Because of this, we have to play strange tricks with have_echo, to ensure
101 # that the top-level statement containing the test starts after the 'alias'
103 if test -z "$have_echo"; then
110 alias echo=bsd_echo 2>/dev/null
112 if test -z "$have_echo" \
113 && echo '\t' | grep t > /dev/null; then
116 if test -z "$have_echo"; then
117 unalias echo 2>/dev/null
119 # For Solaris /bin/sh and OSF/1 /bin/sh: respawn using /bin/ksh.
120 if test -z "$have_echo" \
121 && test "X$1" != "X--no-reexec" \
122 && test -f /bin/ksh; then
123 exec /bin/ksh "$0" --no-reexec "$@"
126 # Otherwise: respawn using /bin/sh.
127 if test -z "$have_echo" \
128 && test "X$1" != "X--no-reexec" \
129 && test -f /bin/sh; then
130 exec /bin/sh "$0" --no-reexec "$@"
133 if test -z "$have_echo"; then
134 func_fatal_error "Shell does not support 'echo' correctly. Please install GNU bash and set the environment variable CONFIG_SHELL to point to it."
136 if echo '\t' | grep t > /dev/null; then
139 func_fatal_error "Shell does not support 'echo' correctly. Workaround does not work. Please report this as a bug to bug-gnulib@gnu.org."
141 if test "X$1" = "X--no-reexec"; then
145 # This command determines the exit code.
146 echo '\t' | grep t > /dev/null