| Summary: | /bin/sh: improve echo -e builtin | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Product: | Base System | Reporter: | Helge Oldach <freebsd> | ||||||||
| Component: | bin | Assignee: | freebsd-bugs (Nobody) <bugs> | ||||||||
| Status: | Closed Not A Bug | ||||||||||
| Severity: | Affects Many People | CC: | jilles, rb | ||||||||
| Priority: | --- | ||||||||||
| Version: | 13.1-STABLE | ||||||||||
| Hardware: | Any | ||||||||||
| OS: | Any | ||||||||||
| Attachments: |
|
||||||||||
Created attachment 235435 [details]
235434: naive fix to echo -n -e
Created attachment 235436 [details]
naive fix to echo -n -e
It is indeed unfortunate that /bin/echo and /bin/sh's echo builtin behave differently, but it is documented (sh(1) describes the echo builtin, and echo(1) warns that shell builtins may differ) and changing it now will cause breakage for little benefit. For example, the existing echo builtin allows 'echo -n STRING' for any string, while the proposed patch breaks this for the string "-e". Also note that -ne can be replaced by -e and adding \c to the last operand. An alternative is replacing echo use with an appropriate printf command. Just to observe that other builtins have similar oddities, see eg: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=263901 (In reply to Jilles Tjoelker from comment #3) > For example, the existing echo builtin allows 'echo -n STRING' for any string, while the proposed patch breaks this for the string "-e". True, so we shouldn't loop over the arguments but only validate the first argument against being a string of options and check for '-en' or '-ne'. This will also keep it in line with other built-ins. > Also note that -ne can be replaced by -e and adding \c to the last operand. True as well, but then the '-e' option and the '\\' detection logic could be removed as it's undocumented anyhow... |
Created attachment 235434 [details] naive fix to echo -n -e The "echo" builtin has an (undocumented) escape sequence expansion feature, called for by the '-e' flag. Unfortunately that collides with the (documented) '-n' flag, as the builtin only checks the first flag on the command line. The patch adds a naive fix to this, making this snippet DTRT: echo -n -e '\a' A proper fix should probably employ getopt.