spraff
spraff

Reputation: 33405

How can I make bash realpath work with variables?

I run

#!/bin/bash

id="~/.ssh/foo"

realpath ~/.ssh/foo
realpath "~/.ssh/foo"
realpath $id
realpath "$id"

expecting 4 identical lines of output, with no errors. What I get is

/home/me/.ssh/foo
realpath: '~/.ssh/foo': No such file or directory
realpath: '~/.ssh/foo': No such file or directory
realpath: '~/.ssh/foo': No such file or directory

Why does only the first of these succeed?

I want to do

echo "Enter a path: "
read p
p="$(realpath "$p")"
echo "The real path is $p"

which doesn't work because of this error. What do I need to do?

Upvotes: 0

Views: 1119

Answers (0)

Related Questions