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: Tim Visher
Subject: Re: difference in how ~ and HOME are treated when trying to remove a matching prefix.
Date: Tue, 2 Mar 2021 09:51:51 -0500

On Tue, Mar 2, 2021 at 9:43 AM Cristian Zoicas <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


reply via email to

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