-1

Background

I'm using the Digital Ocean API to create a new server (droplet) and including a bash script to be automatically run when the server starts up the first time.

Problem

In my bash script (which runs as root), I try to install Oh-My-Zsh via its shell script, which tries to install it in ~. However, when I ssh into the machine, I find that Oh-My-Zsh has been installed into /~/ instead of /root.

Question

What could be causing bash to interpret ~ as the name of a folder instead of as an alias for $HOME, and is there anything I can do to fix this? The Oh My Zsh install script is not owned by me, so I can't simply change each ~ to $HOME or /root (of course I could hack together a sed command to do the replacement for me in that install script, but it seems like there should be an easier way...)

Christopher Shroba
  • 7,006
  • 8
  • 40
  • 68
  • Show us the context. If the `~` is literal in a variable that's expanded, that would do it, but we'd need to see a specific instance of code that causes that error to speak authoritatively, *included as part of the question*. – Charles Duffy Apr 06 '18 at 14:40
  • Also, whether `HOME` is defined matters. (And, err, you say you're running a oh-my-zsh install script with *bash*, not zsh? There are very different, and not mutually-compatible, shells) – Charles Duffy Apr 06 '18 at 14:41
  • ...example: `var="~/foo"; mkdir -p "$var"` will cause the problem, but that's just one of many possible mechanisms. Without showing the specific code for your individual failure case, this question is just a call for speculation. – Charles Duffy Apr 06 '18 at 14:44
  • (Possible duplicates, from which this question should be [edit]ed to distinguish: [Why is a tilde not expanded in a shell script?](https://stackoverflow.com/questions/32276909/why-is-a-tilde-in-a-path-not-expanded-in-a-shell-script); [Why isn't tilde (~) expanding in double quotes?](https://stackoverflow.com/questions/41871596/why-isnt-tilde-expanding-inside-double-quotes)) – Charles Duffy Apr 06 '18 at 14:46
  • Hey @CharlesDuffy, thanks for commenting! It looks like I blindly assumed `$HOME` would be defined, and it was not. Once I manually defined it, everything worked as intended. Thank you very much for your help! – Christopher Shroba Apr 06 '18 at 14:57
  • Something is very wrong if `HOME` is not defined. From section 8.3, Other Environment Variables, of the POSIX spec: "The system shall initialize [HOME] at the time of login to be a pathname of the user's home directory." – chepner Apr 06 '18 at 15:21

1 Answers1

2

This can happen several ways. To take a few:

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441