Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

`printf -v foo ""` does not set foo=

23 views
Skip to first unread message

Mike Frysinger

unread,
Jun 17, 2013, 1:27:06 AM6/17/13
to bug-...@gnu.org
simple test code:
unset foo
printf -v foo ""
echo ${foo+set}

that does not display "set". seems to have been this way since the feature
was added in bash-3.1.
-mike
signature.asc

Chris Down

unread,
Jun 17, 2013, 1:41:51 AM6/17/13
to Mike Frysinger, bug-bash
Interesting. It also won't change it if it already exists, it seems:

$ foo=bar
$ printf -v foo ""
$ echo "$foo"
bar
$ echo "$BASH_VERSION"
4.2.45(2)-release

Linda Walsh

unread,
Jun 17, 2013, 2:33:52 AM6/17/13
to bug-...@gnu.org
Mike Frysinger wrote:
> simple test code:
> unset foo
> printf -v foo ""
> echo ${foo+set}
>
> that does not display "set". seems to have been this way since the feature
> was added in bash-3.1.
> -mike
----
Indeed:
> set -u
> unset foo
> printf -v foo ""
> echo $foo
bash: foo: unbound variable
> foo=""
> echo $foo
----
I have a feeling this would be hard to fix, since how can printf
tell the difference between
printf -v foo ""
and
printf -v foo
??

(with nothing after it?) it seems the semantic parser would have already
removed the quotes by the time the args are passed to printf, even this:
> set -u
> printf -v foo "$(echo "$'\000'")"
> echo $foo

still leaves foo gutless: without content (even if were null)



Andreas Schwab

unread,
Jun 17, 2013, 3:29:17 AM6/17/13
to Linda Walsh, bug-...@gnu.org
Linda Walsh <ba...@tlinx.org> writes:

> I have a feeling this would be hard to fix, since how can printf
> tell the difference between
> printf -v foo ""
> and
> printf -v foo
> ??

The latter has $# -eq 2.

Andreas.

--
Andreas Schwab, SUSE Labs, sch...@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE 1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."

John Kearney

unread,
Jun 17, 2013, 6:09:26 AM6/17/13
to Linda Walsh, bug-...@gnu.org
Thats one of the reasons I suggested the following syntax
printf -v test "%s" ""
It doesn't have this problem it also saves other problems as well.
of if you want to expand back slashes etc.
printf -v test "%b" ""
Gesendet: Montag, 17. Juni 2013 um 08:33 Uhr
Von: "Linda Walsh" <ba...@tlinx.org>
An: bug-...@gnu.org
Betreff: Re: `printf -v foo ""` does not set foo=
Mike Frysinger wrote:
> simple test code:
> unset foo
> printf -v foo ""
> echo ${foo+set}
>
> that does not display "set". seems to have been this way since the
feature
> was added in bash-3.1.
> -mike
----
Indeed:
> set -u
> unset foo
> printf -v foo ""
> echo $foo
bash: foo: unbound variable
> foo=""
> echo $foo
----
I have a feeling this would be hard to fix, since how can printf
tell the difference between
printf -v foo ""
and
printf -v foo
??

Chet Ramey

unread,
Jun 24, 2013, 4:13:01 PM6/24/13
to Mike Frysinger, bug-...@gnu.org, chet....@case.edu
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 6/17/13 1:27 AM, Mike Frysinger wrote:
> simple test code:
> unset foo
> printf -v foo ""
> echo ${foo+set}
>
> that does not display "set". seems to have been this way since the feature
> was added in bash-3.1.

printf returns immediately if the format string is null. It has always
been implemented this way.

Chet
- --
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU ch...@case.edu http://cnswww.cns.cwru.edu/~chet/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlHIqEwACgkQu1hp8GTqdKuxWgCfWco3J9SOm/N+pQh8BNC64be4
ngkAn2d4BsB2lkvRpRTCIK5iqZgEguo+
=92+u
-----END PGP SIGNATURE-----

Mike Frysinger

unread,
Jun 24, 2013, 6:52:55 PM6/24/13
to chet....@case.edu, bug-...@gnu.org
On Monday 24 June 2013 16:13:01 Chet Ramey wrote:
> On 6/17/13 1:27 AM, Mike Frysinger wrote:
> > simple test code:
> > unset foo
> > printf -v foo ""
> > echo ${foo+set}
> >
> > that does not display "set". seems to have been this way since the
> > feature was added in bash-3.1.
>
> printf returns immediately if the format string is null. It has always
> been implemented this way.

seems like when the -v arg is in use, that [otherwise reasonable] shortcut
should be not taken ?
-mike
signature.asc

Chet Ramey

unread,
Jun 25, 2013, 9:53:59 AM6/25/13
to Mike Frysinger, bug-...@gnu.org, chet....@case.edu
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Maybe. I'll take a look. I suspect that the amount of code that depends
on the current behavior is pretty small.

Chet
- --
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU ch...@case.edu http://cnswww.cns.cwru.edu/~chet/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iEYEARECAAYFAlHJoPcACgkQu1hp8GTqdKskTQCfatq1LQuEh6oPEl55Ljqarf1T
HGsAnA6fPLj+bP8LIqeSbayFne6HOkR4
=9mwl
-----END PGP SIGNATURE-----

0 new messages