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