1

I am trying to find an absolute path of a supplied relative path, and use a default one if one was not supplied

Originally I had OUTPUT_PATH=${OUTP:-"/home/default/output/dir"} I can do OUTPUT_PATH="$( cd "$(dirname ${OUTP})" && pwd)" to get the absolute path

but if I combine the two to:

OUTPUT_PATH=${"$( cd "$(dirname ${OUTP})" && pwd)":-"/home/default/output/dir"}

I am getting a bad substitution error, why is that?

Quantico
  • 2,398
  • 7
  • 35
  • 59

1 Answers1

3

The ${varname:-default} notation means "substitute the value of the variable named varname, if it's set and non-empty; otherwise, substitute the string default".

In your case, "$( cd "$(dirname ${OUTP})" && pwd)" is not the name of a variable, so ${"$( cd "$(dirname ${OUTP})" && pwd)":-"/home/default/output/dir"} is not using the above notation; it's just gibberish.

Also, the dirname call doesn't make sense to me; I think you might be misunderstanding what that utility does.

Overall, I think what you want is:

OUTPUT_PATH="$(cd "${OUTP:-/home/default/output/dir}" && pwd)"

You'll also want some error-checking afterward, to ensure that $OUTPUT_PATH is actually set (i.e., that cd was able to move to the specified directory).

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • It would be worthwhile to note bash's `-L` and `-P` options for the `cd` command. "Absolute path" could mean a couple of different things. Oh, and +1 for "just gibberish". :-) – ghoti Mar 04 '16 at 16:53