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: Cristian Zoicas
Subject: Re: difference in how ~ and HOME are treated when trying to remove a matching prefix.
Date: Tue, 2 Mar 2021 16:00:00 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0 SeaMonkey/2.53.3



Tim Visher wrote:
On Tue, Mar 2, 2021 at 9:43 AM Cristian Zoicas <zoicas@medialab.sissa.it 
<mailto:zoicas@medialab.sissa.it>> wrote:

    Hello all

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

    # ~ 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////

    # The prefix is removed with all following characters (due to *)
    #
    wj@rogers:~$ (A="/home/wj////"; echo "A (with prefix)   : ${A}"; echo "A 
(without prefix): ${A##${HOME}*}")
    A (with prefix)   : /home/wj////
    A (without prefix):

    Is this a bug or am I missing something?


I think you may be misunderstanding Tilde expansion. Tilde expansion only 
occurs when the tilde is at the beginning of a word or during variable 
assignment when it follows any `:` character or the first `=` character. The 
reasons for the `:` character are because of its prevalence in variables like 
`PATH`. Thus the `~` character is _not_ a variable like `HOME` and can't be 
used in all the contexts that a variable can be used, one of which is Parameter 
Expansion.

https://www.gnu.org/software/bash/manual/bash.html#Tilde-Expansion
https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion

The explanation is interesting, but I would add the following:

In the first case, the prefix is nto removed because the ~ is found in an 
innapropriate place. For example,
if i add a / after the ~, the prefix is removed:

(A="/home/wj////"; echo "A (with prefix)   : ${A}"; echo "A (without prefix): 
${A##~/*}")
A (with prefix)   : /home/wj////
A (without prefix):

According to these examples (in the firs email and in this email) what is going 
on is
that ~ followed by * does not work but if there is a / between ~ and * then it 
works.

Cristian




reply via email to

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