]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/echo.def
bash-5.0 distribution sources and documentation
[thirdparty/bash.git] / builtins / echo.def
CommitLineData
726f6388
JA
1This file is echo.def, from which is created echo.c.
2It implements the builtin "echo" in Bash.
3
d233b485 4Copyright (C) 1987-2018 Free Software Foundation, Inc.
726f6388
JA
5
6This file is part of GNU Bash, the Bourne Again SHell.
7
3185942a
JA
8Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
726f6388 12
3185942a
JA
13Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
726f6388 17
3185942a
JA
18You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
726f6388
JA
20
21$PRODUCES echo.c
ccc6cda3
JA
22#include <config.h>
23
24#if defined (HAVE_UNISTD_H)
25# include <unistd.h>
26#endif
27
d166f048
JA
28#include "../bashansi.h"
29
726f6388
JA
30#include <stdio.h>
31#include "../shell.h"
32
0628567a
JA
33#include "common.h"
34
726f6388
JA
35$BUILTIN echo
36$FUNCTION echo_builtin
37$DEPENDS_ON V9_ECHO
38$SHORT_DOC echo [-neE] [arg ...]
3185942a
JA
39Write arguments to the standard output.
40
ac50fbac
CR
41Display the ARGs, separated by a single space character and followed by a
42newline, on the standard output.
3185942a
JA
43
44Options:
45 -n do not append a newline
46 -e enable interpretation of the following backslash escapes
47 -E explicitly suppress interpretation of backslash escapes
48
49`echo' interprets the following backslash-escaped characters:
50 \a alert (bell)
51 \b backspace
52 \c suppress further output
53 \e escape character
ac50fbac 54 \E escape character
3185942a
JA
55 \f form feed
56 \n new line
57 \r carriage return
58 \t horizontal tab
59 \v vertical tab
60 \\ backslash
61 \0nnn the character whose ASCII code is NNN (octal). NNN can be
a0c0a00f 62 0 to 3 octal digits
3185942a 63 \xHH the eight-bit character whose value is HH (hexadecimal). HH
a0c0a00f 64 can be one or two hex digits
d233b485
CR
65 \uHHHH the Unicode character whose value is the hexadecimal value HHHH.
66 HHHH can be one to four hex digits.
67 \UHHHHHHHH the Unicode character whose value is the hexadecimal value
68 HHHHHHHH. HHHHHHHH can be one to eight hex digits.
3185942a
JA
69
70Exit Status:
71Returns success unless a write error occurs.
726f6388
JA
72$END
73
74$BUILTIN echo
75$FUNCTION echo_builtin
76$DEPENDS_ON !V9_ECHO
77$SHORT_DOC echo [-n] [arg ...]
3185942a
JA
78Write arguments to the standard output.
79
80Display the ARGs on the standard output followed by a newline.
81
82Options:
83 -n do not append a newline
84
85Exit Status:
86Returns success unless a write error occurs.
726f6388
JA
87$END
88
89#if defined (V9_ECHO)
90# define VALID_ECHO_OPTIONS "neE"
91#else /* !V9_ECHO */
92# define VALID_ECHO_OPTIONS "n"
93#endif /* !V9_ECHO */
94
bb70624e
JA
95/* System V machines already have a /bin/sh with a v9 behaviour. We
96 give Bash the identical behaviour for these machines so that the
97 existing system shells won't barf. Regrettably, the SUS v2 has
98 standardized the Sys V echo behavior. This variable is external
99 so that we can have a `shopt' variable to control it at runtime. */
95732b49 100#if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX)
bb70624e
JA
101int xpg_echo = 1;
102#else
103int xpg_echo = 0;
104#endif /* DEFAULT_ECHO_TO_XPG */
105
726f6388
JA
106/* Print the words in LIST to standard output. If the first word is
107 `-n', then don't print a trailing newline. We also support the
ccc6cda3
JA
108 echo syntax from Version 9 Unix systems. */
109int
726f6388
JA
110echo_builtin (list)
111 WORD_LIST *list;
112{
d166f048
JA
113 int display_return, do_v9, i, len;
114 char *temp, *s;
726f6388 115
bb70624e 116 do_v9 = xpg_echo;
ccc6cda3 117 display_return = 1;
726f6388 118
95732b49
JA
119 if (posixly_correct && xpg_echo)
120 goto just_echo;
121
ccc6cda3
JA
122 for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
123 {
726f6388
JA
124 /* If it appears that we are handling options, then make sure that
125 all of the options specified are actually valid. Otherwise, the
126 string should just be echoed. */
ccc6cda3 127 temp++;
726f6388
JA
128
129 for (i = 0; temp[i]; i++)
130 {
131 if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
ccc6cda3 132 break;
726f6388
JA
133 }
134
ccc6cda3
JA
135 /* echo - and echo -<nonopt> both mean to just echo the arguments. */
136 if (*temp == 0 || temp[i])
137 break;
726f6388
JA
138
139 /* All of the options in TEMP are valid options to ECHO.
140 Handle them. */
ccc6cda3 141 while (i = *temp++)
726f6388 142 {
ccc6cda3
JA
143 switch (i)
144 {
145 case 'n':
146 display_return = 0;
147 break;
726f6388 148#if defined (V9_ECHO)
ccc6cda3
JA
149 case 'e':
150 do_v9 = 1;
151 break;
152 case 'E':
153 do_v9 = 0;
154 break;
726f6388 155#endif /* V9_ECHO */
ccc6cda3
JA
156 default:
157 goto just_echo; /* XXX */
158 }
726f6388 159 }
726f6388
JA
160 }
161
162just_echo:
163
b80f6443
JA
164 clearerr (stdout); /* clear error before writing and testing success */
165
ccc6cda3 166 while (list)
726f6388 167 {
d166f048 168 i = len = 0;
bb70624e 169 temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len)
ccc6cda3
JA
170 : list->word->word;
171 if (temp)
726f6388 172 {
d166f048
JA
173 if (do_v9)
174 {
175 for (s = temp; len > 0; len--)
176 putchar (*s++);
177 }
178 else
179 printf ("%s", temp);
180#if defined (SunOS5)
ccc6cda3 181 fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */
d166f048 182#endif
726f6388 183 }
a0c0a00f 184 QUIT;
ccc6cda3
JA
185 if (do_v9 && temp)
186 free (temp);
187 list = list->next;
188 if (i)
189 {
190 display_return = 0;
191 break;
192 }
193 if (list)
194 putchar(' ');
a0c0a00f 195 QUIT;
726f6388 196 }
ccc6cda3 197
726f6388 198 if (display_return)
ccc6cda3 199 putchar ('\n');
3185942a 200
3185942a 201 return (sh_chkwrite (EXECUTION_SUCCESS));
726f6388 202}