0

I'm trying to get a path from user input and use it as a variable in a command:

read -e -p "Path to backup: " path
mongodump --host localhost --port 27017 --db my-db --out $path

When I put there ~/here as a script variable command is never successful (no error). When I put the exactly same value the command directly it is working:

read -e -p "Path to backup: " path
mongodump --host localhost --port 27017 --db my-db --out ~/here

When I print echo "$path" it prints ~/here.

Any ideas?

Thanks

  • 1
    The tilde is not expanded when you read it into a variable from standard input like that. Your code would be cleaner (and easier to use) if you allowed the user to give the pathname on the command line of the script instead (where the tilde would be expanded). Also, double quote all variable expansions. – Kusalananda May 18 at 11:47
  • Thank you! Worked! – o..o May 18 at 12:00

Browse other questions tagged or ask your own question.