[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How does bash interpret tilde character?



On Sun, Aug 10, 2025 at 7:23 AM Vincent Lefevre <vincent@vinc17.net> wrote:
> BTW, a difference between shells:
> With bash:
>
> $ echo ~ foo=~/bar:~/rod
> /home/vinc17 foo=/home/vinc17/bar:/home/vinc17/rod

Less about shells and more about compliance.

$ echo ~ foo=~/bar:~/rod
/home/nexus foo=/home/nexus/bar:/home/nexus/rod

$ set -o posix
$ echo ~ foo=~/bar:~/rod
/home/nexus foo=~/bar:~/rod

>From the last paragraph of the Tilde Expansion section (on my machine):

> Bash also performs tilde expansion on words satisfying the conditions of
> variable assignments (as described above under PARAMETERS) when they ap‐
> pear as arguments to simple commands.  Bash does not do this, except for
> the declaration commands listed above, when in posix mode.

I'm surprised neither checkbashisms(1) nor shellcheck(1) flags that in
a simple shell script.

========

Also, I just learned some more tilde tricks.

I've long known about ~USER:

$ echo ~root ~nobody
/root /nonexistent

But I just learned about ~+ and ~- being similar to $PWD and $OLDPWD.

$ cd /tmp
$ echo ~+
/tmp
$ echo ~-
/home/nexus

Not quite the same though, as in a new shell:
$ echo ~-
~-
$ echo ==${OLDPWD}--   # extra chars to demonstrate empty string
==--


Reply to: