0

I've been digging through the internet and I see examples of replacing $HOME with ~, but I'm trying to go the other way (e.g. - replace ~ with $HOME and currently if I try to run this:

if [[ $directory_name = *~* ]]; then
  echo "${$directory_name/\~/$HOME}"
fi

to replace it, I get this error:

${$directory_name/\~/$HOME}: bad substitution

I have #!/bin/bash at the top of my script file and when I run it I've been using something like this:

sh test-script.sh

I'm also doing this in terminal on a Mac, so I'm not sure if that has anything to do with it.

Again...new to bash scripting so while this seems logical, I could be going about this all wrong and missing something. Thanks!

ultraloveninja
  • 1,969
  • 5
  • 27
  • 56
  • 1
    `echo "${directory_name/\~/$HOME}"` – rici Jun 25 '18 at 02:49
  • 2
    You also probably don't want `*~*`. `~` should only be replaced with `$HOME` if it forms the complete first directory component. `~user/` is the home directory of user `user`. Otherwise, `~` is just a character. – rici Jun 25 '18 at 02:52
  • ...there are other special cases -- `~-` has a non-`$HOME` meaning, for example; so does `~+`. – Charles Duffy Jun 25 '18 at 03:12
  • I know its closed but if someone else comes to this I Want them to see an answer echo "~" | sed "s/^~/${HOME//\//\\/}/ig" the ig is not needed just a habit – Ron Jun 30 '21 at 16:32

0 Answers0