On Thu 2021-06-17 10:55:27 -0400, J C Nash wrote:
> While I've found a workaround, I was a bit surprised that a test for 
> existence of
> a directory in a bash script did not work.

This is POSIX shell in general.

> I tried
>
> DIR="~/Something/"

Just DIR="$HOME/Something" unless you need to refer to someone else's
home.

> if [ -d "$DIR" ]; then

Heck, I'd just if [ -d ~/Something ]; then

> and got that the directory did NOT exist when it was clearly there.
>
> Workaround was to use
>
> DIR="/home/$(basename ~)/Something/"
>
> I did a bit of a search, but found no mention of the fact that ~ is not
> expanded in the if [  ] construct. Is it, in fact, "well-known" except
> to me?

It's because the tilde is in quotes.

See 2018 POSIX shell command language ยง 2.6.1 Tilde Expansion

    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>.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_01

Your tilde in your command substitution is unquoted, which is why it
works.

To unsubscribe send a blank message to linux+unsubscr...@linux-ottawa.org
To get help send a blank message to linux+h...@linux-ottawa.org
To visit the archives: https://lists.linux-ottawa.org

Reply via email to