1 This file is echo.def, from which is created echo.c.
2 It implements the builtin "echo" in Bash.
4 Copyright (C) 1987-2009 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 #if defined (HAVE_UNISTD_H)
28 #include "../bashansi.h"
36 $FUNCTION echo_builtin
38 $SHORT_DOC echo [-neE] [arg ...]
39 Write arguments to the standard output.
41 Display the ARGs on the standard output followed by a newline.
44 -n do not append a newline
45 -e enable interpretation of the following backslash escapes
46 -E explicitly suppress interpretation of backslash escapes
48 `echo' interprets the following backslash-escaped characters:
51 \c suppress further output
59 \0nnn the character whose ASCII code is NNN (octal). NNN can be
61 \xHH the eight-bit character whose value is HH (hexadecimal). HH
62 can be one or two hex digits
65 Returns success unless a write error occurs.
69 $FUNCTION echo_builtin
71 $SHORT_DOC echo [-n] [arg ...]
72 Write arguments to the standard output.
74 Display the ARGs on the standard output followed by a newline.
77 -n do not append a newline
80 Returns success unless a write error occurs.
84 # define VALID_ECHO_OPTIONS "neE"
86 # define VALID_ECHO_OPTIONS "n"
89 /* System V machines already have a /bin/sh with a v9 behaviour. We
90 give Bash the identical behaviour for these machines so that the
91 existing system shells won't barf. Regrettably, the SUS v2 has
92 standardized the Sys V echo behavior. This variable is external
93 so that we can have a `shopt' variable to control it at runtime. */
94 #if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX)
98 #endif /* DEFAULT_ECHO_TO_XPG */
100 extern int posixly_correct;
102 /* Print the words in LIST to standard output. If the first word is
103 `-n', then don't print a trailing newline. We also support the
104 echo syntax from Version 9 Unix systems. */
109 int display_return, do_v9, i, len;
115 if (posixly_correct && xpg_echo)
118 for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
120 /* If it appears that we are handling options, then make sure that
121 all of the options specified are actually valid. Otherwise, the
122 string should just be echoed. */
125 for (i = 0; temp[i]; i++)
127 if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
131 /* echo - and echo -<nonopt> both mean to just echo the arguments. */
132 if (*temp == 0 || temp[i])
135 /* All of the options in TEMP are valid options to ECHO.
144 #if defined (V9_ECHO)
153 goto just_echo; /* XXX */
160 clearerr (stdout); /* clear error before writing and testing success */
162 terminate_immediately++;
166 temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len)
172 for (s = temp; len > 0; len--)
178 fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */
196 terminate_immediately--;
197 return (sh_chkwrite (EXECUTION_SUCCESS));