0

I have a script where I am trying to have a user enter the PATH to a command. And most of the time, this command is going to live in the user directory, so more often than not, the PATH will be entered using the ~ sign.

The code looks essentially like this (for simplicity of debugging):

echo -n "Enter PATH to start-site command and press [ENTER]: "
read startSite

cd $startSite
./start-site.sh $repositoryName $DIR/..

but I just get an error saying ~/my/path isn't a file or directory. If I copy and paste the command from the error into the terminal, it works fine.

Any ideas?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Kenyon
  • 807
  • 1
  • 12
  • 24
  • `eval cd $startSite` – Cyrus Nov 07 '16 at 18:49
  • Awesome, that worked. Thanks! – Kenyon Nov 07 '16 at 18:50
  • @Kenyon, be extremely careful with `eval` -- suppose the user enters `$(rm -f /my/valuable/directory/)`: once you eval that, your valuable files are deleted. Might be better to do: `read startSite; startSite=$(sed "s,^~,$HOME," <<<"$startSite"); cd "$startSite"` – glenn jackman Nov 07 '16 at 19:09
  • I'm not really worried about it. It's just a utility command for like 3 people in the office. I don't think any of them are going to try rm -rf ='ing there computer from a tool designed to create a new site boilerplate haha xD – Kenyon Nov 07 '16 at 19:57
  • Unquoted `startSite` will only work if the directory name doesn't contain any spaces or glob characters, though. – chepner Nov 07 '16 at 21:44

0 Answers0