1 /* echo.c, derived from code echo.c in Bash.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #include <sys/types.h>
24 /* The official name of this program (e.g., no 'g' prefix). */
25 #define PROGRAM_NAME "echo"
28 proper_name ("Brian Fox"), \
29 proper_name ("Chet Ramey")
31 /* If true, interpret backslash escapes by default. */
32 #ifndef DEFAULT_ECHO_TO_XPG
33 enum { DEFAULT_ECHO_TO_XPG
= false };
39 /* STATUS should always be EXIT_SUCCESS (unlike in most other
40 utilities which would call emit_try_help otherwise). */
41 affirm (status
== EXIT_SUCCESS
);
44 Usage: %s [SHORT-OPTION]... [STRING]...\n\
46 "), program_name
, program_name
);
48 Echo the STRING(s) to standard output.\n\
50 -n do not output the trailing newline\n\
52 fputs (_(DEFAULT_ECHO_TO_XPG
54 -e enable interpretation of backslash escapes (default)\n\
55 -E disable interpretation of backslash escapes\n")
57 -e enable interpretation of backslash escapes\n\
58 -E disable interpretation of backslash escapes (default)\n")),
60 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
61 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
64 If -e is in effect, the following sequences are recognized:\n\
71 \\c produce no further output\n\
75 \\r carriage return\n\
80 \\0NNN byte with octal value NNN (1 to 3 digits)\n\
81 \\xHH byte with hexadecimal value HH (1 to 2 digits)\n\
83 printf (USAGE_BUILTIN_WARNING
, PROGRAM_NAME
);
85 Consider using the 'printf' command instead,\n\
86 as it avoids problems when outputting option-like strings.\n\
88 emit_ancillary_info (PROGRAM_NAME
);
92 /* Convert C from hexadecimal character to integer. */
94 hextobin (unsigned char c
)
98 default: return c
- '0';
99 case 'a': case 'A': return 10;
100 case 'b': case 'B': return 11;
101 case 'c': case 'C': return 12;
102 case 'd': case 'D': return 13;
103 case 'e': case 'E': return 14;
104 case 'f': case 'F': return 15;
108 /* Print the words in LIST to standard output. If the first word is
109 '-n', then don't print a trailing newline. We also support the
110 echo syntax from Version 9 unix systems. */
113 main (int argc
, char **argv
)
115 bool display_return
= true;
116 bool posixly_correct
= !!getenv ("POSIXLY_CORRECT");
119 || (! DEFAULT_ECHO_TO_XPG
&& 1 < argc
&& STREQ (argv
[1], "-n")));
121 /* System V machines already have a /bin/sh with a v9 behavior.
122 Use the identical behavior for these machines so that the
123 existing system shell scripts won't barf. */
124 bool do_v9
= DEFAULT_ECHO_TO_XPG
;
126 initialize_main (&argc
, &argv
);
127 set_program_name (argv
[0]);
128 setlocale (LC_ALL
, "");
129 bindtextdomain (PACKAGE
, LOCALEDIR
);
130 textdomain (PACKAGE
);
132 atexit (close_stdout
);
134 /* We directly parse options, rather than use parse_long_options, in
135 order to avoid accepting abbreviations. */
136 if (allow_options
&& argc
== 2)
138 if (STREQ (argv
[1], "--help"))
139 usage (EXIT_SUCCESS
);
141 if (STREQ (argv
[1], "--version"))
143 version_etc (stdout
, PROGRAM_NAME
, PACKAGE_NAME
, Version
, AUTHORS
,
153 while (argc
> 0 && *argv
[0] == '-')
155 char const *temp
= argv
[0] + 1;
158 /* If it appears that we are handling options, then make sure that
159 all of the options specified are actually valid. Otherwise, the
160 string should just be echoed. */
162 for (i
= 0; temp
[i
]; i
++)
165 case 'e': case 'E': case 'n':
174 /* All of the options in TEMP are valid options to ECHO.
188 display_return
= false;
198 if (do_v9
|| posixly_correct
)
202 char const *s
= argv
[0];
211 case 'a': c
= '\a'; break;
212 case 'b': c
= '\b'; break;
213 case 'c': return EXIT_SUCCESS
;
214 case 'e': c
= '\x1B'; break;
215 case 'f': c
= '\f'; break;
216 case 'n': c
= '\n'; break;
217 case 'r': c
= '\r'; break;
218 case 't': c
= '\t'; break;
219 case 'v': c
= '\v'; break;
222 unsigned char ch
= *s
;
223 if (! c_isxdigit (ch
))
231 c
= c
* 16 + hextobin (ch
);
237 if (! ('0' <= *s
&& *s
<= '7'))
241 case '1': case '2': case '3':
242 case '4': case '5': case '6': case '7':
244 if ('0' <= *s
&& *s
<= '7')
245 c
= c
* 8 + (*s
++ - '0');
246 if ('0' <= *s
&& *s
<= '7')
247 c
= c
* 8 + (*s
++ - '0');
252 default: putchar ('\\'); break;
267 fputs (argv
[0], stdout
);