bug-bash
[Top][All Lists]
Advanced

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

Re: printf %q represents null argument as empty string.


From: John Kearney
Subject: Re: printf %q represents null argument as empty string.
Date: Sat, 12 Jan 2013 02:35:34 +0100
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0

Am 11.01.2013 22:05, schrieb Dan Douglas:
> On Friday, January 11, 2013 09:39:00 PM John Kearney wrote:
>> Am 11.01.2013 19:38, schrieb Dan Douglas:
>>>     $ set --; printf %q\\n "$@"
>>>     ''
>>>
>>> printf should perhaps only output '' when there is actually a 
> corresponding
>>> empty argument, else eval "$(printf %q ...)" and similar may give 
> different 
>>> results than expected. Other shells don't output '', even mksh's ${var@Q} 
>>> expansion. Zsh's ${(q)var} does.
>> that is not a bug in printf %q
>>
>> it what you expect to happen with "${@}" 
>> should that be 0 arguments if $# is 0.
>>
>> I however find the behavior irritating, but correct from the description.
>>
>> to do what you are suggesting you would need a special case handler for this
>> "${@}" as oposed to "jjjj${@}jjjjj" or any other variation.
>>
>>
>> what I tend to do as a workaround is
>>
>> printf() {
>>     if [ $# -eq 2 -a -z "${2}" ];then
>>         builtin printf "${1}"
>>     else
>>         builtin printf "${@}"
>>     fi
>> }
>>
>>
>> or not as good but ok in most cases something like
>>
>> printf "%q" ${1:+"${@}"}
>>
>>
> I don't understand what you mean. The issue I'm speaking of is that printf %q 
> produces a quoted empty string both when given no args and when given one 
> empty arg. A quoted "$@" with no positional parameters present expands to 
> zero 
> words (and correspondingly for "${arr[@]}"). Why do you think "x${@}x" is 
> special? (Note that expansion didn't even work correctly a few patchsets ago.)
>
> Also as pointed out, every other shell with a printf %q feature disagrees 
> with 
> Bash. Are you saying that something in the manual says that it should do 
> otherwise? I'm aware you could write a wrapper, I just don't see any utility 
> in the default behavior.


um maybe an example will calrify my  attempted point

set -- arg1 arg2 arg3
set -- "--(${@})--"
printf "<%q> " "${@}"
<--\(arg1> <arg2> <arg3\)-->


set --
set -- "--(${@})--"
printf "<%q> " "${@}"
<--\(\)-->


so there is always at least one word or one arg, just because its "${@}"
should not  affect this behavior.


is that clearer as such bash is doing the right thing as far as I'm
concerned, truthfully its not normally what I want but that is beside
the point consistency is more important, especially when its so easy to
work around.


the relevant part of the man page is
 When there are no  array  members,  ${name[@]}  expands  to nothing.  

<<<HERE>>>>
If  the double-quoted expansion occurs within a word, the expansion of
the first parameter is joined with the beginning part of the
       original word, and the expansion of the last parameter is joined
with the last part of the original word.  This is analogous to the 
expansion
       of  the  special parameters * and @ (see Special Parameters
above).  ${#name[subscript]} expands to the length of
${name[subscript]}.  If sub-
       script is * or @, the expansion is the number of elements in the
array.  Referencing an array variable without a subscript  is 
equivalent  to
       referencing the array with a subscript of 0.


so
set --

printf "%q" "${@}"
becomes
printf "%q" ""

which is correct as ''











reply via email to

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