13

I lost a couple of hours trying to run VNC server (x0vncserver) and the client refused to connect with weird message that

No password configured for VNC Auth

The server also prints this error

 SVncAuth:    opening password file '~/.vnc/passwd' failed

Ok, I wasted a lot of time until I realized the tilde was neither expanded by the shell, nor by x0vncserver. Then I ran these tests

$ echo --PasswordFile=~/.vnc/passwd
--PasswordFile=~/.vnc/passwd

But

$ echo PasswordFile=~/.vnc/passwd
PasswordFile=/home/tichomir/.vnc/passwd

Why is that? Why the shell refuses to expand tilde if the argument begins with a dash? I thought tilde will always expand as long as it is not quoted, but apparently there is another rule that comes into play?

2

1 Answer 1

16

This is a peculiarity of the bash shell described in its manual:

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

This means that bash will expand the tilde in your PasswordFile=~/.vnc/passwd string, since it's an argument to echo that looks like a variable assignment.

The string --PasswordFile=~/.vnc/passwd does not look like a variable assignment since the string --PasswordFile is not a valid variable name.

Note that bash does not do this when running in POSIX mode, and that other shells, like zsh, ksh or yash do not do this by default (zsh has a magicequalsubst option for tilde expansion to be performed after unquoted equal signs (=) though).

If you want to ensure that the home directory path of the current user is properly expanded as part of an argument to a command, use the $HOME value instead of the tilde:

echo --PasswordFile="$HOME/.vnc/passwd"

The "declaration commands listed above" referred to in the manual are the built in commands alias, declare, typeset, export, readonly, and local.

6
  • 1
    +1 | I would not think of that. Commented Feb 3, 2019 at 10:23
  • Though note: bash --posix -c '"export" a=~; printf "%s\n" "$a"' outputs ~. Commented Feb 3, 2019 at 10:29
  • 2
    Note that the ~ being expanded in alias a=~ would be a POSIX conformance bug (and is not useful). But that's how ksh88 did it (that changed in ksh93) and is probably why bash, zsh and pdksh do it as well. Why yash which was written against the POSIX spec doesn't do it. Commented Feb 3, 2019 at 10:38
  • This is the right answer, but the right approach would have been to just supply the option argument as a separate argument, rather than merged with the option using = into one argument. Then the tilde expansion is at the beginning of a word and the question is moot.
    – JdeBP
    Commented Feb 3, 2019 at 12:35
  • 1
    @JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file. Commented Feb 3, 2019 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .