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, separated by a single space character and followed by a
42 newline, on the standard output.
45 -n do not append a newline
46 -e enable interpretation of the following backslash escapes
47 -E explicitly suppress interpretation of backslash escapes
49 `echo' interprets the following backslash-escaped characters:
52 \c suppress further output
61 \0nnn the character whose ASCII code is NNN (octal). NNN can be
63 \xHH the eight-bit character whose value is HH (hexadecimal). HH
64 can be one or two hex digits
67 Returns success unless a write error occurs.
71 $FUNCTION echo_builtin
73 $SHORT_DOC echo [-n] [arg ...]
74 Write arguments to the standard output.
76 Display the ARGs on the standard output followed by a newline.
79 -n do not append a newline
82 Returns success unless a write error occurs.
86 # define VALID_ECHO_OPTIONS "neE"
88 # define VALID_ECHO_OPTIONS "n"
91 /* System V machines already have a /bin/sh with a v9 behaviour. We
92 give Bash the identical behaviour for these machines so that the
93 existing system shells won't barf. Regrettably, the SUS v2 has
94 standardized the Sys V echo behavior. This variable is external
95 so that we can have a `shopt' variable to control it at runtime. */
96 #if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX)
100 #endif /* DEFAULT_ECHO_TO_XPG */
102 extern int posixly_correct;
104 /* Print the words in LIST to standard output. If the first word is
105 `-n', then don't print a trailing newline. We also support the
106 echo syntax from Version 9 Unix systems. */
111 int display_return, do_v9, i, len;
117 if (posixly_correct && xpg_echo)
120 for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
122 /* If it appears that we are handling options, then make sure that
123 all of the options specified are actually valid. Otherwise, the
124 string should just be echoed. */
127 for (i = 0; temp[i]; i++)
129 if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
133 /* echo - and echo -<nonopt> both mean to just echo the arguments. */
134 if (*temp == 0 || temp[i])
137 /* All of the options in TEMP are valid options to ECHO.
146 #if defined (V9_ECHO)
155 goto just_echo; /* XXX */
162 clearerr (stdout); /* clear error before writing and testing success */
164 terminate_immediately++;
168 temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len)
174 for (s = temp; len > 0; len--)
180 fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */
198 terminate_immediately--;
199 return (sh_chkwrite (EXECUTION_SUCCESS));