Should ~$user be tilde expanded?

2012-10-25 Thread Clark WANG
See following example:

$ cat foo.sh
u=root
echo ~$u
$ bash foo.sh  # bash 4.2.37
~root
$ ksh foo.sh  # ksh 93u+ 2012-08-01
/root
$

Anyone can explain/confirm what should be the correct behavior?

-Clark


Re: Should ~$user be tilde expanded?

2012-10-25 Thread Roman Rakus

On 10/25/2012 04:07 PM, Clark WANG wrote:

See following example:

$ cat foo.sh
u=root
echo ~$u
$ bash foo.sh  # bash 4.2.37
~root
$ ksh foo.sh  # ksh 93u+ 2012-08-01
/root
$

Anyone can explain/confirm what should be the correct behavior?

-Clark

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Expansions

quoting:
The order of expansions is: brace expansion, tilde expansion, parameter, 
variable, and arithmetic expansion and command substitution (done in a 
left-to-right fashion), word splitting, and filename expansion.


And posix is saying:
The order of word expansion shall be as follows:

1. Tilde expansion (see Section 2.6.1), parameter expansion (see Section 
2.6.2, on page 2306),
  command substitution (see Section 2.6.3, on page 2309), and 
arithmetic expansion (see

 Section 2.6.4, on page 2299).


So I will say, bash is doing it right. Firstly tries tilde expansion and 
then parameter expansion.


RR



Re: [ast-users] [ksh93] Should ~$user be tilde expanded?

2012-10-25 Thread Chet Ramey
On 10/25/12 9:43 AM, David Korn wrote:
> cc: ast-us...@research.att.com
> Subject: Re: [ast-users]  [ksh93] Should ~$user be tilde expanded?
> 
> 
> 
>> I think bash behavior is correct according to
>> http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_
>> 06which
>> says tilde expansion should be done before parameter expansion.
>>
> 
> Where do you see that?  In section 2.6, it says
> 
> "Tilde expansion (see Tilde Expansion), parameter expansion (see Parameter
> Expansion), command substitution (see Command Substitution), and arithmetic
> expansion (see Arithmetic Expansion) shall be performed, beginning to end.
> See item 5 in Token Recognition.
> 
> which implies that these should be simultaneously from beginning to end.

But ksh isn't doing that.  It's taking the $u and saying `this tilde prefix
is a potential login name' and then applying expansions to that potential
login name.  You have to apply some ordering here, and ksh chooses a
different one than bash.  The standard doesn't make it explicit about what,
if any, expansions are performed on the login name, so both behaviors may
in fact be conformant.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: [ast-users] [ksh93] Should ~$user be tilde expanded?

2012-10-25 Thread Dan Douglas
For reference, the only current shell I can find that agrees with ksh93 is zsh 
in its default mode. In all emulation modes (including ksh), it reverts to 
being like Bash and others (mksh, dash, busybox, posh).

Regardless of which is correct, I've seen a lot of questions and confusion 
generated by the more common behavior. 
--
Dan Douglas