0

In a bash script, I want the user to enter a path to an application, and then launch that application. By default, the application is expected to be in the user's own directory. The following script works for me on Mac OS X, but it is not particularly elegant:

path_default="~/neo4j/bin/neo4j"
read -p "Enter path to neo4j [$path_default]: " path
path="${path:-$path_default}"

if [[ ${path:0:1} == "~" ]]; then
  path="/Users/$USER"${path#"~"}
fi

$path start

How can I improve this so that it will work on any platform?

James Newton
  • 6,623
  • 8
  • 49
  • 113

2 Answers2

0

if you consider ~ can be in any platform you can use a variable, for example userpath=$(echo ~)

Asenar
  • 6,732
  • 3
  • 36
  • 49
0

Try this:

path_default="$HOME/bin/neo4j"
read -p "Enter path to neo4j [$path_default]: " path
path="${path:-$path_default}"
path="${path/#\~/$HOME}"

Here, tilde in the beginning of the path variable is substituted by the contents of $HOME variable.

Vasily G
  • 859
  • 8
  • 16