Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Some programs do not recognize tilde shortcut in filenames and expect full canonical paths, e.g. /home/dave/myfile.txt instead of ~/myfile.txt.

Is there a way to make bash replace ~ and ~user by full canonical paths anytime a tilde is used?

share|improve this question
Can you provide examples of commands where ~ isn't expanded by bash ? The shell should do it. Called programs do not need to interpret it. – jlliagre Nov 1 '10 at 8:26
Consider replacing ~ with $HOME as well. – frabjous Nov 1 '10 at 14:04
For example, some Perl scripts won't recognize paths with ~. So what I would like bash to do is explicitly expand the path, just like it auto completes when I hit the tab key. – David B Nov 1 '10 at 19:55

Well, not sure about perl, but in bash script you can do that conversion with eval:

eval dir='~user/somedir'

$dir will contain full path, i.e. /home/user/somedir at this point.

share|improve this answer

When you run a command in the Bash shell, like:

command ~/myfile.txt

the shell first makes the expansion of ~ (unless it's quoted) then it run the command with the result.

But it's true, some programs don't interpret the ~, for example:

$ cat "~/myfile.txt"
~/myfile.txt: No such file or directory

but this works insted:

nano "~/myfile.txt"

But this behavior is masked by the Bash expansion, so you don't need any further interpretation.

Maybe it's obvious, but note that if a program uses a configuration files and expects some paths in it, it's not a Bash duty expand them.

share|improve this answer

You must log in to answer this question.

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