bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bug: incorrect variable values when reading from file with 0d0a as n


From: Greg Wooledge
Subject: Re: bug: incorrect variable values when reading from file with 0d0a as newlines
Date: Tue, 17 Dec 2013 10:04:35 -0500
User-agent: Mutt/1.4.2.3i

On Tue, Dec 17, 2013 at 03:18:31PM +0200, Sergey Skripnick wrote:
> Description:
>     When executed script (by `source' or by dot syntax) which have 0d0a  
> characters as a newlines, readed variables have incorrect values. As you  
> can see below, 0d character is included,

This is expected.

> but there is no 0d character  
> between the quotes.

> VAR1="ok" # unused
> VAR="ok"
> VAR3="ok" # unused

The value on the right hand side of the = in an assignment does not need
to be contained within quotes.  The 0x0d character that follows the quotes
becomes part of the value, just as if you had written

VAR="foo"bar

As far as Bash is concerned, 0x0d is just a normal character, like x or 7.

> echo $VAR | hexdump -C

Failure to quote the expansion "$VAR" may lead to confusion in the
future.  0x0d is not part of IFS, so in this particular case there
was no problem, but it will eventually bite you if you continue to
neglect it.

echo "$VAR" | hexdump -C

Even better would be to use printf %s, so that an extra newline is not
added by echo.

printf %s "$VAR" | hexdump -C

Or, Bash-specific:

declare -p VAR



reply via email to

[Prev in Thread] Current Thread [Next in Thread]