]> git.ipfire.org Git - thirdparty/bash.git/blame - builtins/echo.def
Bash-4.1 patchlevel 11
[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
3185942a 4Copyright (C) 1987-2009 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
41Display the ARGs on the standard output followed by a newline.
42
43Options:
44 -n do not append a newline
45 -e enable interpretation of the following backslash escapes
46 -E explicitly suppress interpretation of backslash escapes
47
48`echo' interprets the following backslash-escaped characters:
49 \a alert (bell)
50 \b backspace
51 \c suppress further output
52 \e escape character
53 \f form feed
54 \n new line
55 \r carriage return
56 \t horizontal tab
57 \v vertical tab
58 \\ backslash
59 \0nnn the character whose ASCII code is NNN (octal). NNN can be
60 0 to 3 octal digits
61 \xHH the eight-bit character whose value is HH (hexadecimal). HH
62 can be one or two hex digits
63
64Exit Status:
65Returns success unless a write error occurs.
726f6388
JA
66$END
67
68$BUILTIN echo
69$FUNCTION echo_builtin
70$DEPENDS_ON !V9_ECHO
71$SHORT_DOC echo [-n] [arg ...]
3185942a
JA
72Write arguments to the standard output.
73
74Display the ARGs on the standard output followed by a newline.
75
76Options:
77 -n do not append a newline
78
79Exit Status:
80Returns success unless a write error occurs.
726f6388
JA
81$END
82
83#if defined (V9_ECHO)
84# define VALID_ECHO_OPTIONS "neE"
85#else /* !V9_ECHO */
86# define VALID_ECHO_OPTIONS "n"
87#endif /* !V9_ECHO */
88
bb70624e
JA
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. */
95732b49 94#if defined (DEFAULT_ECHO_TO_XPG) || defined (STRICT_POSIX)
bb70624e
JA
95int xpg_echo = 1;
96#else
97int xpg_echo = 0;
98#endif /* DEFAULT_ECHO_TO_XPG */
99
95732b49
JA
100extern int posixly_correct;
101
726f6388
JA
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
ccc6cda3
JA
104 echo syntax from Version 9 Unix systems. */
105int
726f6388
JA
106echo_builtin (list)
107 WORD_LIST *list;
108{
d166f048
JA
109 int display_return, do_v9, i, len;
110 char *temp, *s;
726f6388 111
bb70624e 112 do_v9 = xpg_echo;
ccc6cda3 113 display_return = 1;
726f6388 114
95732b49
JA
115 if (posixly_correct && xpg_echo)
116 goto just_echo;
117
ccc6cda3
JA
118 for (; list && (temp = list->word->word) && *temp == '-'; list = list->next)
119 {
726f6388
JA
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. */
ccc6cda3 123 temp++;
726f6388
JA
124
125 for (i = 0; temp[i]; i++)
126 {
127 if (strchr (VALID_ECHO_OPTIONS, temp[i]) == 0)
ccc6cda3 128 break;
726f6388
JA
129 }
130
ccc6cda3
JA
131 /* echo - and echo -<nonopt> both mean to just echo the arguments. */
132 if (*temp == 0 || temp[i])
133 break;
726f6388
JA
134
135 /* All of the options in TEMP are valid options to ECHO.
136 Handle them. */
ccc6cda3 137 while (i = *temp++)
726f6388 138 {
ccc6cda3
JA
139 switch (i)
140 {
141 case 'n':
142 display_return = 0;
143 break;
726f6388 144#if defined (V9_ECHO)
ccc6cda3
JA
145 case 'e':
146 do_v9 = 1;
147 break;
148 case 'E':
149 do_v9 = 0;
150 break;
726f6388 151#endif /* V9_ECHO */
ccc6cda3
JA
152 default:
153 goto just_echo; /* XXX */
154 }
726f6388 155 }
726f6388
JA
156 }
157
158just_echo:
159
b80f6443
JA
160 clearerr (stdout); /* clear error before writing and testing success */
161
3185942a 162 terminate_immediately++;
ccc6cda3 163 while (list)
726f6388 164 {
d166f048 165 i = len = 0;
bb70624e 166 temp = do_v9 ? ansicstr (list->word->word, STRLEN (list->word->word), 1, &i, &len)
ccc6cda3
JA
167 : list->word->word;
168 if (temp)
726f6388 169 {
d166f048
JA
170 if (do_v9)
171 {
172 for (s = temp; len > 0; len--)
173 putchar (*s++);
174 }
175 else
176 printf ("%s", temp);
177#if defined (SunOS5)
ccc6cda3 178 fflush (stdout); /* Fix for bug in SunOS 5.5 printf(3) */
d166f048 179#endif
726f6388 180 }
ccc6cda3
JA
181 if (do_v9 && temp)
182 free (temp);
183 list = list->next;
184 if (i)
185 {
186 display_return = 0;
187 break;
188 }
189 if (list)
190 putchar(' ');
726f6388 191 }
ccc6cda3 192
726f6388 193 if (display_return)
ccc6cda3 194 putchar ('\n');
3185942a
JA
195
196 terminate_immediately--;
197 return (sh_chkwrite (EXECUTION_SUCCESS));
726f6388 198}