why newline scape sequence does not work in Freebsd's bash
Dan Nelson
dnelson at allantgroup.com
Sat Dec 31 05:56:42 UTC 2011
In the last episode (Dec 31), Erik Trulsson said:
> On Fri, Dec 30, 2011 at 06:47:45PM -0800, Edward Martinez wrote:
> > On 12/30/11 17:06, ??????? ???????? wrote:
> > > I used ' singe quotes, so double quotes is:
> > >
> > > $ FRUIT_BASKET="apples oranges pears"
> > > $ echo -e "My fruit basket contains: \n $FRUIT_BASKET"
> > > My fruit basket contains:
> > > apples oranges pears
> >
> > Thanks for the help, it worked. I find it interesting that FreeBSD's
> > echo man page does not mention the -e option is needed to enable
> > backslash escapes. I remembered why it worked on linux is because i
> > created an echo alias with the -e option. So i will do the same for
> > FreeBSD.
>
> The echo(1) manpage on FreeBSD doesn't say anything about '-e' because
> that version of echo doesn't have such an option. The echo you were
> actually using is the one builtin into bash and is described in the
> bash(1) manpage (including mention of the -e option.)
If you want a command guaranteed to process backslash-escape sequences, use
the printf command, not echo.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html
It is not possible to use echo portably across all POSIX systems
unless both -n (as the first argument) and escape sequences are
omitted.
The printf utility can be used portably to emulate any of the
traditional behaviors of the echo utility as follows (assuming that
IFS has its standard value or is unset).
New applications are encouraged to use printf instead of echo.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/printf.html
The %b conversion specification is not part of the ISO C standard;
it has been added here as a portable way to process
<backslash>-escapes expanded in string operands as provided by the
echo utility. See also the APPLICATION USAGE section of echo for
ways to use printf as a replacement for all of the traditional
versions of the echo utility.
--
Dan Nelson
dnelson at allantgroup.com
More information about the freebsd-questions
mailing list