0

as the home directory?

I tried all 3 methods below but it just echos out the ~.

NAME="~/_root/repo/"

echo $NAME
echo "$NAME"
echo "${NAME}
Sun - FE
  • 469

2 Answers2

2

Have the shell expand the ~ before assigning it to NAME, by removing the quotes on the assignment statement.

NAME=~/_root/repo/
echo "$NAME"
AndyB
  • 180
1

There's no function for that. A tilde inside a string is just a tilde. You'll have to manually match and replace it with the value of $HOME:

var=${var}/
var=${var/#"~/"/"$HOME/"}
var=${var%/}

If you use 'eval', it interprets everything – it expands the tilde, but it also expands wildcards, it also expands variable substitutions, it also parses spaces, quotes, array syntax, and so on.

var="~/dir/file.txt"
eval "var=$var"

var="~/dir with spaces/file.txt"
eval "var=$var"

var="~/dir/file (1).txt"
eval "var=$var"
grawity
  • 501,077