help-bash
[Top][All Lists]
Advanced

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

Re: difference in how ~ and HOME are treated when trying to remove a mat


From: Chet Ramey
Subject: Re: difference in how ~ and HOME are treated when trying to remove a matching prefix.
Date: Tue, 2 Mar 2021 10:46:39 -0500
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.8.0

On 3/2/21 9:43 AM, Cristian Zoicas wrote:
Hello all

There is a difference in how ~ and HOME are treated when trying to
remove a matching prefix.

Yes, there is. They are not the same thing.


# ~ and $HOME are the same:
#
wj@rogers:~$ echo ~
/home/wj
wj@rogers:~$ echo $HOME
/home/wj


# Nothing is removed (although the entire contents should be removed).
#
wj@rogers:~$ (A="/home/wj////"; echo "A (with prefix)   : ${A}"; echo "A (without prefix): ${A##~*}")
A (with prefix)   : /home/wj////
A (without prefix): /home/wj////

This is the result of a misunderstanding about what constitutes a "tilde
prefix." POSIX says the following:

"A "tilde-prefix" consists of an unquoted <tilde> character at the
beginning of a word, followed by all of the characters preceding the first
unquoted <slash> in the word, or all the characters in the word if there is
no <slash>."

In this case, the word is "~*". There is no slash. The tilde-prefix is "~*", which means that the shell tries to look up a user `*', since

"If none of the characters in the tilde-prefix are quoted, the characters
in the tilde-prefix following the <tilde> are treated as a possible login
name from the user database."

That fails, of course, so the tilde prefix is left unchanged. The pattern
is `~*', including the literal tilde, which doesn't match anything in $A,
so the expansion doesn't remove anything.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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