[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: |
Greg Wooledge |
Subject: |
Re: difference in how ~ and HOME are treated when trying to remove a matching prefix. |
Date: |
Tue, 2 Mar 2021 10:50:48 -0500 |
Cristian Zoicas (zoicas@medialab.sissa.it) wrote:
> 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.
Tilde expansion is quite peculiar, yes. The exact rules for how it behaves
are different across shells, too.
Bash is a lot more lenient about when it performs tilde expansion than
other POSIX shells are (e.g. dash). In this particular case, bash
performs the tilde expansion when it sees ~ as a solo word, or ~ followed
by a / character.
unicorn:~$ x=/home/greg/foobar/baz; echo "${x##~/f}"
oobar/baz
unicorn:~$ x=/home/greg/foobar/baz; echo "${x##~}"
/foobar/baz
unicorn:~$ x=/home/greg/foobar/baz; echo "${x##~*}"
/home/greg/foobar/baz
But the ~ followed by an * character does not qualify, so the tilde
expansion is not performed there.
Personally, I would recommend not relying on tilde expansion inside the
patterns used by parameter expansion.