Why doesn't the tilde (~) expand inside double quotes?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








49















According to this answer and my own understanding, the tilde expands to the home directory:



$ echo ~
/home/braiam


Now, whenever I want the shell expansion to work, i. e. using variable names such $FOO, and do not break due unexpected characters, such spaces, etc. one should use double quotes ":



$ FOO="some string with spaces"
$ BAR="echo $FOO"
$ echo $BAR
echo some string with spaces


Why doesn't this expansion works with the tilde?



$ echo ~/some/path
/home/braiam/some/path
$ echo "~/some/path"
~/some/path









share|improve this question



















  • 1





    pubs.opengroup.org/onlinepubs/009695399/utilities/…

    – llua
    Aug 24 '14 at 1:12






  • 3





    Also note that this has the inconsistency when providing a program argument on command line that on command argument --path ~/myfile expands but --path=~/myfile doesn't.

    – Ángel
    Aug 24 '14 at 18:59











  • Related: Does ~ always equal $HOME

    – Stéphane Chazelas
    Aug 18 '16 at 10:41











  • Related (on Ask Ubuntu): Why does mkdir fail (no such file or directory) in a script with BIN_DIR=“~/bin/”?

    – Eliah Kagan
    Nov 17 '17 at 12:49











  • A variation on this theme is unix.stackexchange.com/questions/279565 .

    – JdeBP
    Jan 26 '18 at 16:38

















49















According to this answer and my own understanding, the tilde expands to the home directory:



$ echo ~
/home/braiam


Now, whenever I want the shell expansion to work, i. e. using variable names such $FOO, and do not break due unexpected characters, such spaces, etc. one should use double quotes ":



$ FOO="some string with spaces"
$ BAR="echo $FOO"
$ echo $BAR
echo some string with spaces


Why doesn't this expansion works with the tilde?



$ echo ~/some/path
/home/braiam/some/path
$ echo "~/some/path"
~/some/path









share|improve this question



















  • 1





    pubs.opengroup.org/onlinepubs/009695399/utilities/…

    – llua
    Aug 24 '14 at 1:12






  • 3





    Also note that this has the inconsistency when providing a program argument on command line that on command argument --path ~/myfile expands but --path=~/myfile doesn't.

    – Ángel
    Aug 24 '14 at 18:59











  • Related: Does ~ always equal $HOME

    – Stéphane Chazelas
    Aug 18 '16 at 10:41











  • Related (on Ask Ubuntu): Why does mkdir fail (no such file or directory) in a script with BIN_DIR=“~/bin/”?

    – Eliah Kagan
    Nov 17 '17 at 12:49











  • A variation on this theme is unix.stackexchange.com/questions/279565 .

    – JdeBP
    Jan 26 '18 at 16:38













49












49








49


13






According to this answer and my own understanding, the tilde expands to the home directory:



$ echo ~
/home/braiam


Now, whenever I want the shell expansion to work, i. e. using variable names such $FOO, and do not break due unexpected characters, such spaces, etc. one should use double quotes ":



$ FOO="some string with spaces"
$ BAR="echo $FOO"
$ echo $BAR
echo some string with spaces


Why doesn't this expansion works with the tilde?



$ echo ~/some/path
/home/braiam/some/path
$ echo "~/some/path"
~/some/path









share|improve this question
















According to this answer and my own understanding, the tilde expands to the home directory:



$ echo ~
/home/braiam


Now, whenever I want the shell expansion to work, i. e. using variable names such $FOO, and do not break due unexpected characters, such spaces, etc. one should use double quotes ":



$ FOO="some string with spaces"
$ BAR="echo $FOO"
$ echo $BAR
echo some string with spaces


Why doesn't this expansion works with the tilde?



$ echo ~/some/path
/home/braiam/some/path
$ echo "~/some/path"
~/some/path






shell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 13 '17 at 2:22









Reid

24849




24849










asked Aug 24 '14 at 0:57









BraiamBraiam

23.8k2077142




23.8k2077142







  • 1





    pubs.opengroup.org/onlinepubs/009695399/utilities/…

    – llua
    Aug 24 '14 at 1:12






  • 3





    Also note that this has the inconsistency when providing a program argument on command line that on command argument --path ~/myfile expands but --path=~/myfile doesn't.

    – Ángel
    Aug 24 '14 at 18:59











  • Related: Does ~ always equal $HOME

    – Stéphane Chazelas
    Aug 18 '16 at 10:41











  • Related (on Ask Ubuntu): Why does mkdir fail (no such file or directory) in a script with BIN_DIR=“~/bin/”?

    – Eliah Kagan
    Nov 17 '17 at 12:49











  • A variation on this theme is unix.stackexchange.com/questions/279565 .

    – JdeBP
    Jan 26 '18 at 16:38












  • 1





    pubs.opengroup.org/onlinepubs/009695399/utilities/…

    – llua
    Aug 24 '14 at 1:12






  • 3





    Also note that this has the inconsistency when providing a program argument on command line that on command argument --path ~/myfile expands but --path=~/myfile doesn't.

    – Ángel
    Aug 24 '14 at 18:59











  • Related: Does ~ always equal $HOME

    – Stéphane Chazelas
    Aug 18 '16 at 10:41











  • Related (on Ask Ubuntu): Why does mkdir fail (no such file or directory) in a script with BIN_DIR=“~/bin/”?

    – Eliah Kagan
    Nov 17 '17 at 12:49











  • A variation on this theme is unix.stackexchange.com/questions/279565 .

    – JdeBP
    Jan 26 '18 at 16:38







1




1





pubs.opengroup.org/onlinepubs/009695399/utilities/…

– llua
Aug 24 '14 at 1:12





pubs.opengroup.org/onlinepubs/009695399/utilities/…

– llua
Aug 24 '14 at 1:12




3




3





Also note that this has the inconsistency when providing a program argument on command line that on command argument --path ~/myfile expands but --path=~/myfile doesn't.

– Ángel
Aug 24 '14 at 18:59





Also note that this has the inconsistency when providing a program argument on command line that on command argument --path ~/myfile expands but --path=~/myfile doesn't.

– Ángel
Aug 24 '14 at 18:59













Related: Does ~ always equal $HOME

– Stéphane Chazelas
Aug 18 '16 at 10:41





Related: Does ~ always equal $HOME

– Stéphane Chazelas
Aug 18 '16 at 10:41













Related (on Ask Ubuntu): Why does mkdir fail (no such file or directory) in a script with BIN_DIR=“~/bin/”?

– Eliah Kagan
Nov 17 '17 at 12:49





Related (on Ask Ubuntu): Why does mkdir fail (no such file or directory) in a script with BIN_DIR=“~/bin/”?

– Eliah Kagan
Nov 17 '17 at 12:49













A variation on this theme is unix.stackexchange.com/questions/279565 .

– JdeBP
Jan 26 '18 at 16:38





A variation on this theme is unix.stackexchange.com/questions/279565 .

– JdeBP
Jan 26 '18 at 16:38










4 Answers
4






active

oldest

votes


















40














The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal.



POSIX defines Double-Quotes as:




Enclosing characters in double-quotes ( "" ) shall preserve the
literal value of all characters within the double-quotes, with the
exception of the characters dollar sign, backquote, and backslash,



...



The application shall ensure that a double-quote is preceded by a
backslash to be included within double-quotes. The parameter '@' has
special meaning inside double-quotes




Except $, `, and @, others characters are treated as literal inside double quotes.






share|improve this answer




















  • 9





    In which case I guess you should use $HOME.

    – Seth
    Aug 24 '14 at 20:29






  • 11





    Or you can just not quote the ~, for example ls -l ~/"My Documents"

    – Andrew Medico
    Aug 24 '14 at 21:47












  • This is very non-intuitive. What is the reason they chose to do this? (This answer doesn't actually give the reason, but rather just points to the standard, but presumably the standard was written that way for a reason.)

    – iconoclast
    Jan 16 '15 at 2:56






  • 1





    @iconoclast if you really want a "why they got implemented this way" read Stephane answer instead.

    – Braiam
    Mar 8 '15 at 16:46






  • 2





    So, @iconoclast, why is the sky blue? :) :) :)

    – Jesse Chisholm
    Mar 28 '16 at 18:10


















30














Tilde expansion is defined by POSIX as:




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>. In an assignment, multiple tilde-prefixes can be used: [...] following the <equals-sign> of the assignment, following any unquoted <colon>, or both. [...] If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the <tilde> are treated as a possible login name from the user database. [...] If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable HOME. If HOME is unset, the results are unspecified. [...]




So the shortest answer is "because it's defined that way": quoting any of the characters in the prefix, including the ~, suppresses expansion.



It also defines the expansion as always resulting in a single word, so quoting would be unnecessary:




The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.




Where some of the path requires quoting, but the rest is a tilde prefix, you can combine tilde expansion and ordinary quoting straightforwardly:



$ cat ~/"file name with spaces"


On the broader "why": since there's no conceivable use for word-splitting ~, that should be the default behaviour, rather than requiring it to be quoted. Because there's no need to quote it, giving ~ a special meaning inside quotes would be an unnecessary complication. And, of course, historical reasons mean it couldn't be changed now even if that were desirable.






share|improve this answer

























  • According to this bash guide, tilde expansion happens before whitespace separation. Is there a way to safely do a tilde expansion, even if you home directory has whitespace in it? Ordinarily, of course, you'd do this sort of thing with "".

    – Lucretiel
    Aug 21 '15 at 18:53












  • The expansion is a single word; see the second quoted passage in the answer.

    – Michael Homer
    Aug 21 '15 at 20:38


















22














~ originates in the C-shell, long before it was added to the Korn shell and later added to the POSIX shell specification.



In the C-shell, ~ was a globbing operator (expanded by the same routine as the one expanding *.txt for instance), so like the rest of the globbing was not performed inside double quotes.






share|improve this answer






























    10














    While this doesn't answer why it's designed that way, you use $HOME instead if you need to substitute, since that's essentially what ~ does.



    $ echo "$HOME/some/path"
    /home/braiam/some/path





    share|improve this answer


















    • 6





      this doesn't work with ~otheruser

      – Johannes Kuhn
      Aug 24 '14 at 10:41






    • 2





      True, but you could do: THEM=~otheruser then use "$THEM/some/path"

      – melds
      Sep 1 '14 at 6:27







    • 1





      The workaround for ~otheruser shows what a bad idea it was to treat ~ differently than variables and other things that are expanded inside double quotes.

      – iconoclast
      Jan 16 '15 at 2:59











    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%2f151850%2fwhy-doesnt-the-tilde-expand-inside-double-quotes%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    40














    The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal.



    POSIX defines Double-Quotes as:




    Enclosing characters in double-quotes ( "" ) shall preserve the
    literal value of all characters within the double-quotes, with the
    exception of the characters dollar sign, backquote, and backslash,



    ...



    The application shall ensure that a double-quote is preceded by a
    backslash to be included within double-quotes. The parameter '@' has
    special meaning inside double-quotes




    Except $, `, and @, others characters are treated as literal inside double quotes.






    share|improve this answer




















    • 9





      In which case I guess you should use $HOME.

      – Seth
      Aug 24 '14 at 20:29






    • 11





      Or you can just not quote the ~, for example ls -l ~/"My Documents"

      – Andrew Medico
      Aug 24 '14 at 21:47












    • This is very non-intuitive. What is the reason they chose to do this? (This answer doesn't actually give the reason, but rather just points to the standard, but presumably the standard was written that way for a reason.)

      – iconoclast
      Jan 16 '15 at 2:56






    • 1





      @iconoclast if you really want a "why they got implemented this way" read Stephane answer instead.

      – Braiam
      Mar 8 '15 at 16:46






    • 2





      So, @iconoclast, why is the sky blue? :) :) :)

      – Jesse Chisholm
      Mar 28 '16 at 18:10















    40














    The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal.



    POSIX defines Double-Quotes as:




    Enclosing characters in double-quotes ( "" ) shall preserve the
    literal value of all characters within the double-quotes, with the
    exception of the characters dollar sign, backquote, and backslash,



    ...



    The application shall ensure that a double-quote is preceded by a
    backslash to be included within double-quotes. The parameter '@' has
    special meaning inside double-quotes




    Except $, `, and @, others characters are treated as literal inside double quotes.






    share|improve this answer




















    • 9





      In which case I guess you should use $HOME.

      – Seth
      Aug 24 '14 at 20:29






    • 11





      Or you can just not quote the ~, for example ls -l ~/"My Documents"

      – Andrew Medico
      Aug 24 '14 at 21:47












    • This is very non-intuitive. What is the reason they chose to do this? (This answer doesn't actually give the reason, but rather just points to the standard, but presumably the standard was written that way for a reason.)

      – iconoclast
      Jan 16 '15 at 2:56






    • 1





      @iconoclast if you really want a "why they got implemented this way" read Stephane answer instead.

      – Braiam
      Mar 8 '15 at 16:46






    • 2





      So, @iconoclast, why is the sky blue? :) :) :)

      – Jesse Chisholm
      Mar 28 '16 at 18:10













    40












    40








    40







    The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal.



    POSIX defines Double-Quotes as:




    Enclosing characters in double-quotes ( "" ) shall preserve the
    literal value of all characters within the double-quotes, with the
    exception of the characters dollar sign, backquote, and backslash,



    ...



    The application shall ensure that a double-quote is preceded by a
    backslash to be included within double-quotes. The parameter '@' has
    special meaning inside double-quotes




    Except $, `, and @, others characters are treated as literal inside double quotes.






    share|improve this answer















    The reason, because inside double quotes, tilde ~ has no special meaning, it's treated as literal.



    POSIX defines Double-Quotes as:




    Enclosing characters in double-quotes ( "" ) shall preserve the
    literal value of all characters within the double-quotes, with the
    exception of the characters dollar sign, backquote, and backslash,



    ...



    The application shall ensure that a double-quote is preceded by a
    backslash to be included within double-quotes. The parameter '@' has
    special meaning inside double-quotes




    Except $, `, and @, others characters are treated as literal inside double quotes.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 24 '14 at 20:27









    Lucio

    5491816




    5491816










    answered Aug 24 '14 at 5:09









    cuonglmcuonglm

    106k25211308




    106k25211308







    • 9





      In which case I guess you should use $HOME.

      – Seth
      Aug 24 '14 at 20:29






    • 11





      Or you can just not quote the ~, for example ls -l ~/"My Documents"

      – Andrew Medico
      Aug 24 '14 at 21:47












    • This is very non-intuitive. What is the reason they chose to do this? (This answer doesn't actually give the reason, but rather just points to the standard, but presumably the standard was written that way for a reason.)

      – iconoclast
      Jan 16 '15 at 2:56






    • 1





      @iconoclast if you really want a "why they got implemented this way" read Stephane answer instead.

      – Braiam
      Mar 8 '15 at 16:46






    • 2





      So, @iconoclast, why is the sky blue? :) :) :)

      – Jesse Chisholm
      Mar 28 '16 at 18:10












    • 9





      In which case I guess you should use $HOME.

      – Seth
      Aug 24 '14 at 20:29






    • 11





      Or you can just not quote the ~, for example ls -l ~/"My Documents"

      – Andrew Medico
      Aug 24 '14 at 21:47












    • This is very non-intuitive. What is the reason they chose to do this? (This answer doesn't actually give the reason, but rather just points to the standard, but presumably the standard was written that way for a reason.)

      – iconoclast
      Jan 16 '15 at 2:56






    • 1





      @iconoclast if you really want a "why they got implemented this way" read Stephane answer instead.

      – Braiam
      Mar 8 '15 at 16:46






    • 2





      So, @iconoclast, why is the sky blue? :) :) :)

      – Jesse Chisholm
      Mar 28 '16 at 18:10







    9




    9





    In which case I guess you should use $HOME.

    – Seth
    Aug 24 '14 at 20:29





    In which case I guess you should use $HOME.

    – Seth
    Aug 24 '14 at 20:29




    11




    11





    Or you can just not quote the ~, for example ls -l ~/"My Documents"

    – Andrew Medico
    Aug 24 '14 at 21:47






    Or you can just not quote the ~, for example ls -l ~/"My Documents"

    – Andrew Medico
    Aug 24 '14 at 21:47














    This is very non-intuitive. What is the reason they chose to do this? (This answer doesn't actually give the reason, but rather just points to the standard, but presumably the standard was written that way for a reason.)

    – iconoclast
    Jan 16 '15 at 2:56





    This is very non-intuitive. What is the reason they chose to do this? (This answer doesn't actually give the reason, but rather just points to the standard, but presumably the standard was written that way for a reason.)

    – iconoclast
    Jan 16 '15 at 2:56




    1




    1





    @iconoclast if you really want a "why they got implemented this way" read Stephane answer instead.

    – Braiam
    Mar 8 '15 at 16:46





    @iconoclast if you really want a "why they got implemented this way" read Stephane answer instead.

    – Braiam
    Mar 8 '15 at 16:46




    2




    2





    So, @iconoclast, why is the sky blue? :) :) :)

    – Jesse Chisholm
    Mar 28 '16 at 18:10





    So, @iconoclast, why is the sky blue? :) :) :)

    – Jesse Chisholm
    Mar 28 '16 at 18:10













    30














    Tilde expansion is defined by POSIX as:




    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>. In an assignment, multiple tilde-prefixes can be used: [...] following the <equals-sign> of the assignment, following any unquoted <colon>, or both. [...] If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the <tilde> are treated as a possible login name from the user database. [...] If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable HOME. If HOME is unset, the results are unspecified. [...]




    So the shortest answer is "because it's defined that way": quoting any of the characters in the prefix, including the ~, suppresses expansion.



    It also defines the expansion as always resulting in a single word, so quoting would be unnecessary:




    The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.




    Where some of the path requires quoting, but the rest is a tilde prefix, you can combine tilde expansion and ordinary quoting straightforwardly:



    $ cat ~/"file name with spaces"


    On the broader "why": since there's no conceivable use for word-splitting ~, that should be the default behaviour, rather than requiring it to be quoted. Because there's no need to quote it, giving ~ a special meaning inside quotes would be an unnecessary complication. And, of course, historical reasons mean it couldn't be changed now even if that were desirable.






    share|improve this answer

























    • According to this bash guide, tilde expansion happens before whitespace separation. Is there a way to safely do a tilde expansion, even if you home directory has whitespace in it? Ordinarily, of course, you'd do this sort of thing with "".

      – Lucretiel
      Aug 21 '15 at 18:53












    • The expansion is a single word; see the second quoted passage in the answer.

      – Michael Homer
      Aug 21 '15 at 20:38















    30














    Tilde expansion is defined by POSIX as:




    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>. In an assignment, multiple tilde-prefixes can be used: [...] following the <equals-sign> of the assignment, following any unquoted <colon>, or both. [...] If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the <tilde> are treated as a possible login name from the user database. [...] If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable HOME. If HOME is unset, the results are unspecified. [...]




    So the shortest answer is "because it's defined that way": quoting any of the characters in the prefix, including the ~, suppresses expansion.



    It also defines the expansion as always resulting in a single word, so quoting would be unnecessary:




    The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.




    Where some of the path requires quoting, but the rest is a tilde prefix, you can combine tilde expansion and ordinary quoting straightforwardly:



    $ cat ~/"file name with spaces"


    On the broader "why": since there's no conceivable use for word-splitting ~, that should be the default behaviour, rather than requiring it to be quoted. Because there's no need to quote it, giving ~ a special meaning inside quotes would be an unnecessary complication. And, of course, historical reasons mean it couldn't be changed now even if that were desirable.






    share|improve this answer

























    • According to this bash guide, tilde expansion happens before whitespace separation. Is there a way to safely do a tilde expansion, even if you home directory has whitespace in it? Ordinarily, of course, you'd do this sort of thing with "".

      – Lucretiel
      Aug 21 '15 at 18:53












    • The expansion is a single word; see the second quoted passage in the answer.

      – Michael Homer
      Aug 21 '15 at 20:38













    30












    30








    30







    Tilde expansion is defined by POSIX as:




    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>. In an assignment, multiple tilde-prefixes can be used: [...] following the <equals-sign> of the assignment, following any unquoted <colon>, or both. [...] If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the <tilde> are treated as a possible login name from the user database. [...] If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable HOME. If HOME is unset, the results are unspecified. [...]




    So the shortest answer is "because it's defined that way": quoting any of the characters in the prefix, including the ~, suppresses expansion.



    It also defines the expansion as always resulting in a single word, so quoting would be unnecessary:




    The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.




    Where some of the path requires quoting, but the rest is a tilde prefix, you can combine tilde expansion and ordinary quoting straightforwardly:



    $ cat ~/"file name with spaces"


    On the broader "why": since there's no conceivable use for word-splitting ~, that should be the default behaviour, rather than requiring it to be quoted. Because there's no need to quote it, giving ~ a special meaning inside quotes would be an unnecessary complication. And, of course, historical reasons mean it couldn't be changed now even if that were desirable.






    share|improve this answer















    Tilde expansion is defined by POSIX as:




    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>. In an assignment, multiple tilde-prefixes can be used: [...] following the <equals-sign> of the assignment, following any unquoted <colon>, or both. [...] If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the <tilde> are treated as a possible login name from the user database. [...] If the login name is null (that is, the tilde-prefix contains only the tilde), the tilde-prefix is replaced by the value of the variable HOME. If HOME is unset, the results are unspecified. [...]




    So the shortest answer is "because it's defined that way": quoting any of the characters in the prefix, including the ~, suppresses expansion.



    It also defines the expansion as always resulting in a single word, so quoting would be unnecessary:




    The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.




    Where some of the path requires quoting, but the rest is a tilde prefix, you can combine tilde expansion and ordinary quoting straightforwardly:



    $ cat ~/"file name with spaces"


    On the broader "why": since there's no conceivable use for word-splitting ~, that should be the default behaviour, rather than requiring it to be quoted. Because there's no need to quote it, giving ~ a special meaning inside quotes would be an unnecessary complication. And, of course, historical reasons mean it couldn't be changed now even if that were desirable.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 24 '14 at 7:58







    user44370

















    answered Aug 24 '14 at 1:18









    Michael HomerMichael Homer

    50.7k8140177




    50.7k8140177












    • According to this bash guide, tilde expansion happens before whitespace separation. Is there a way to safely do a tilde expansion, even if you home directory has whitespace in it? Ordinarily, of course, you'd do this sort of thing with "".

      – Lucretiel
      Aug 21 '15 at 18:53












    • The expansion is a single word; see the second quoted passage in the answer.

      – Michael Homer
      Aug 21 '15 at 20:38

















    • According to this bash guide, tilde expansion happens before whitespace separation. Is there a way to safely do a tilde expansion, even if you home directory has whitespace in it? Ordinarily, of course, you'd do this sort of thing with "".

      – Lucretiel
      Aug 21 '15 at 18:53












    • The expansion is a single word; see the second quoted passage in the answer.

      – Michael Homer
      Aug 21 '15 at 20:38
















    According to this bash guide, tilde expansion happens before whitespace separation. Is there a way to safely do a tilde expansion, even if you home directory has whitespace in it? Ordinarily, of course, you'd do this sort of thing with "".

    – Lucretiel
    Aug 21 '15 at 18:53






    According to this bash guide, tilde expansion happens before whitespace separation. Is there a way to safely do a tilde expansion, even if you home directory has whitespace in it? Ordinarily, of course, you'd do this sort of thing with "".

    – Lucretiel
    Aug 21 '15 at 18:53














    The expansion is a single word; see the second quoted passage in the answer.

    – Michael Homer
    Aug 21 '15 at 20:38





    The expansion is a single word; see the second quoted passage in the answer.

    – Michael Homer
    Aug 21 '15 at 20:38











    22














    ~ originates in the C-shell, long before it was added to the Korn shell and later added to the POSIX shell specification.



    In the C-shell, ~ was a globbing operator (expanded by the same routine as the one expanding *.txt for instance), so like the rest of the globbing was not performed inside double quotes.






    share|improve this answer



























      22














      ~ originates in the C-shell, long before it was added to the Korn shell and later added to the POSIX shell specification.



      In the C-shell, ~ was a globbing operator (expanded by the same routine as the one expanding *.txt for instance), so like the rest of the globbing was not performed inside double quotes.






      share|improve this answer

























        22












        22








        22







        ~ originates in the C-shell, long before it was added to the Korn shell and later added to the POSIX shell specification.



        In the C-shell, ~ was a globbing operator (expanded by the same routine as the one expanding *.txt for instance), so like the rest of the globbing was not performed inside double quotes.






        share|improve this answer













        ~ originates in the C-shell, long before it was added to the Korn shell and later added to the POSIX shell specification.



        In the C-shell, ~ was a globbing operator (expanded by the same routine as the one expanding *.txt for instance), so like the rest of the globbing was not performed inside double quotes.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 24 '14 at 20:41









        Stéphane ChazelasStéphane Chazelas

        313k57593949




        313k57593949





















            10














            While this doesn't answer why it's designed that way, you use $HOME instead if you need to substitute, since that's essentially what ~ does.



            $ echo "$HOME/some/path"
            /home/braiam/some/path





            share|improve this answer


















            • 6





              this doesn't work with ~otheruser

              – Johannes Kuhn
              Aug 24 '14 at 10:41






            • 2





              True, but you could do: THEM=~otheruser then use "$THEM/some/path"

              – melds
              Sep 1 '14 at 6:27







            • 1





              The workaround for ~otheruser shows what a bad idea it was to treat ~ differently than variables and other things that are expanded inside double quotes.

              – iconoclast
              Jan 16 '15 at 2:59















            10














            While this doesn't answer why it's designed that way, you use $HOME instead if you need to substitute, since that's essentially what ~ does.



            $ echo "$HOME/some/path"
            /home/braiam/some/path





            share|improve this answer


















            • 6





              this doesn't work with ~otheruser

              – Johannes Kuhn
              Aug 24 '14 at 10:41






            • 2





              True, but you could do: THEM=~otheruser then use "$THEM/some/path"

              – melds
              Sep 1 '14 at 6:27







            • 1





              The workaround for ~otheruser shows what a bad idea it was to treat ~ differently than variables and other things that are expanded inside double quotes.

              – iconoclast
              Jan 16 '15 at 2:59













            10












            10








            10







            While this doesn't answer why it's designed that way, you use $HOME instead if you need to substitute, since that's essentially what ~ does.



            $ echo "$HOME/some/path"
            /home/braiam/some/path





            share|improve this answer













            While this doesn't answer why it's designed that way, you use $HOME instead if you need to substitute, since that's essentially what ~ does.



            $ echo "$HOME/some/path"
            /home/braiam/some/path






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 24 '14 at 9:49









            meldsmelds

            1818




            1818







            • 6





              this doesn't work with ~otheruser

              – Johannes Kuhn
              Aug 24 '14 at 10:41






            • 2





              True, but you could do: THEM=~otheruser then use "$THEM/some/path"

              – melds
              Sep 1 '14 at 6:27







            • 1





              The workaround for ~otheruser shows what a bad idea it was to treat ~ differently than variables and other things that are expanded inside double quotes.

              – iconoclast
              Jan 16 '15 at 2:59












            • 6





              this doesn't work with ~otheruser

              – Johannes Kuhn
              Aug 24 '14 at 10:41






            • 2





              True, but you could do: THEM=~otheruser then use "$THEM/some/path"

              – melds
              Sep 1 '14 at 6:27







            • 1





              The workaround for ~otheruser shows what a bad idea it was to treat ~ differently than variables and other things that are expanded inside double quotes.

              – iconoclast
              Jan 16 '15 at 2:59







            6




            6





            this doesn't work with ~otheruser

            – Johannes Kuhn
            Aug 24 '14 at 10:41





            this doesn't work with ~otheruser

            – Johannes Kuhn
            Aug 24 '14 at 10:41




            2




            2





            True, but you could do: THEM=~otheruser then use "$THEM/some/path"

            – melds
            Sep 1 '14 at 6:27






            True, but you could do: THEM=~otheruser then use "$THEM/some/path"

            – melds
            Sep 1 '14 at 6:27





            1




            1





            The workaround for ~otheruser shows what a bad idea it was to treat ~ differently than variables and other things that are expanded inside double quotes.

            – iconoclast
            Jan 16 '15 at 2:59





            The workaround for ~otheruser shows what a bad idea it was to treat ~ differently than variables and other things that are expanded inside double quotes.

            – iconoclast
            Jan 16 '15 at 2:59

















            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%2f151850%2fwhy-doesnt-the-tilde-expand-inside-double-quotes%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

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay