Why tilde (~) doesn't expand when used with CLI argument starting with dash?












9















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?










share|improve this question

























  • Related: Expansion of tilde in zsh

    – Stéphane Chazelas
    Feb 3 at 10:25











  • See also: Does ~ always equal $HOME

    – Stéphane Chazelas
    Feb 3 at 17:20
















9















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?










share|improve this question

























  • Related: Expansion of tilde in zsh

    – Stéphane Chazelas
    Feb 3 at 10:25











  • See also: Does ~ always equal $HOME

    – Stéphane Chazelas
    Feb 3 at 17:20














9












9








9


2






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?










share|improve this question
















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?







bash shell tilde






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 3 at 10:06









Kusalananda

129k16243400




129k16243400










asked Feb 3 at 10:04









Tichomir MitkovTichomir Mitkov

1936




1936













  • Related: Expansion of tilde in zsh

    – Stéphane Chazelas
    Feb 3 at 10:25











  • See also: Does ~ always equal $HOME

    – Stéphane Chazelas
    Feb 3 at 17:20



















  • Related: Expansion of tilde in zsh

    – Stéphane Chazelas
    Feb 3 at 10:25











  • See also: Does ~ always equal $HOME

    – Stéphane Chazelas
    Feb 3 at 17:20

















Related: Expansion of tilde in zsh

– Stéphane Chazelas
Feb 3 at 10:25





Related: Expansion of tilde in zsh

– Stéphane Chazelas
Feb 3 at 10:25













See also: Does ~ always equal $HOME

– Stéphane Chazelas
Feb 3 at 17:20





See also: Does ~ always equal $HOME

– Stéphane Chazelas
Feb 3 at 17:20










1 Answer
1






active

oldest

votes


















13














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.






share|improve this answer





















  • 1





    +1 | I would not think of that.

    – Vlastimil
    Feb 3 at 10:23











  • Though note: bash --posix -c '"export" a=~; printf "%sn" "$a"' outputs ~.

    – Stéphane Chazelas
    Feb 3 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.

    – Stéphane Chazelas
    Feb 3 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
    Feb 3 at 12:35






  • 1





    @JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file.

    – Stéphane Chazelas
    Feb 3 at 17:18











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498410%2fwhy-tilde-doesnt-expand-when-used-with-cli-argument-starting-with-dash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









13














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.






share|improve this answer





















  • 1





    +1 | I would not think of that.

    – Vlastimil
    Feb 3 at 10:23











  • Though note: bash --posix -c '"export" a=~; printf "%sn" "$a"' outputs ~.

    – Stéphane Chazelas
    Feb 3 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.

    – Stéphane Chazelas
    Feb 3 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
    Feb 3 at 12:35






  • 1





    @JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file.

    – Stéphane Chazelas
    Feb 3 at 17:18
















13














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.






share|improve this answer





















  • 1





    +1 | I would not think of that.

    – Vlastimil
    Feb 3 at 10:23











  • Though note: bash --posix -c '"export" a=~; printf "%sn" "$a"' outputs ~.

    – Stéphane Chazelas
    Feb 3 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.

    – Stéphane Chazelas
    Feb 3 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
    Feb 3 at 12:35






  • 1





    @JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file.

    – Stéphane Chazelas
    Feb 3 at 17:18














13












13








13







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 3 at 17:33

























answered Feb 3 at 10:17









KusalanandaKusalananda

129k16243400




129k16243400








  • 1





    +1 | I would not think of that.

    – Vlastimil
    Feb 3 at 10:23











  • Though note: bash --posix -c '"export" a=~; printf "%sn" "$a"' outputs ~.

    – Stéphane Chazelas
    Feb 3 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.

    – Stéphane Chazelas
    Feb 3 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
    Feb 3 at 12:35






  • 1





    @JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file.

    – Stéphane Chazelas
    Feb 3 at 17:18














  • 1





    +1 | I would not think of that.

    – Vlastimil
    Feb 3 at 10:23











  • Though note: bash --posix -c '"export" a=~; printf "%sn" "$a"' outputs ~.

    – Stéphane Chazelas
    Feb 3 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.

    – Stéphane Chazelas
    Feb 3 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
    Feb 3 at 12:35






  • 1





    @JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file.

    – Stéphane Chazelas
    Feb 3 at 17:18








1




1





+1 | I would not think of that.

– Vlastimil
Feb 3 at 10:23





+1 | I would not think of that.

– Vlastimil
Feb 3 at 10:23













Though note: bash --posix -c '"export" a=~; printf "%sn" "$a"' outputs ~.

– Stéphane Chazelas
Feb 3 at 10:29







Though note: bash --posix -c '"export" a=~; printf "%sn" "$a"' outputs ~.

– Stéphane Chazelas
Feb 3 at 10:29






2




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.

– Stéphane Chazelas
Feb 3 at 10:38





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.

– Stéphane Chazelas
Feb 3 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
Feb 3 at 12:35





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
Feb 3 at 12:35




1




1





@JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file.

– Stéphane Chazelas
Feb 3 at 17:18





@JdeBP, as it happens, in the case of x0vncserver, x0vncserver --PasswordFile file doesn't work, you need --PasswordFile=file.

– Stéphane Chazelas
Feb 3 at 17:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f498410%2fwhy-tilde-doesnt-expand-when-used-with-cli-argument-starting-with-dash%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?