1

Tilde is not working inside double quote.what is the reason. Sometimes it's work. There is a two way defile tilde. But the second way is work fine and perfect. What is the real reason?

method 01

#!/bin/sh
location="~/my_documents"
cd "${location}"

method 02

#!/bin/sh
location=~/"my_documents"
cd "${location}"
xuhdev
  • 8,018
  • 2
  • 41
  • 69
waruna k
  • 842
  • 10
  • 20
  • I'm not sure why this has been marked as off-topic by multiple other users. This is very related to Shell programming. – xuhdev Dec 05 '18 at 02:25
  • If it weren't closed as off-topic, it would be eligible to be closed as a duplicate; we have *lots* of Q&A entries from people with bugs caused by the distinction. – Charles Duffy Dec 05 '18 at 02:28
  • ...I'd probably go with [Why is a tilde in a path not expanded in a shell script?](https://stackoverflow.com/questions/32276909/why-is-a-tilde-in-a-path-not-expanded-in-a-shell-script) -- and yes, it's not obvious looking at the titles that the questions cover the same ground; that's actually what makes this a *good* duplicate (one that helps other people find the preexisting canonical question using search terms one flagged) as opposed to a bad one (where someone just didn't bother to search before asking a new question in essentially the same terms). – Charles Duffy Dec 05 '18 at 02:30
  • @xuhdev ...that said, the argument in favor of closure as off-topic (not that I'm taking an a position either way) is that shell questions that are as likely to be stumbled into in interactive use as in scripting are questionable in terms of whether they meet the "**unique to** software development" terms in https://stackoverflow.com/help/on-topic (that condition being ANDed into other constraints); "unique to X" is a much stronger restriction than merely "related to X". – Charles Duffy Dec 05 '18 at 02:32
  • @CharlesDuffy I don't understand why shell scripts are stumbled into interactive use. Shell scripts are pretty common in software development and pretty much everywhere in a POSIX system! For example, shell scripts form 35% percent of code in [Git](https://github.com/git/git). If "unique" is what you are looking for, I don't think JavaScript or CSS is also relevant, because I often change then when as a user in a browser. Those questions should belong to superuser! – xuhdev Dec 05 '18 at 02:40
  • @xuhdev, questions unique to writing shell scripts are clearly on-topic here, but putting `~` in quotes and wondering why it doesn't work happens at the prompt too, not just in scripting. Whether it happens enough at the prompt to justify kicking this question over to [unix.se] is something I'm personally not taking a position on either way -- as I said, I would close it as a duplicate, not as off-topic (and *will* close it as a duplicate, should it be reopened); but I think there's enough of an argument either way that the folks who voted close-as-off-topic had a reasonable rationale. – Charles Duffy Dec 05 '18 at 02:46

1 Answers1

2

Inside quotes, the ~ isn't expanded. It is used as a character

Alberto Moro
  • 1,014
  • 11
  • 22