Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
3  
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

2 Answers 2

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.