0

My directory structure is /local/mnt/abcd/sub 1

So inside the 'sub 1' folder, I am trying to execute the following script

SOURCEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readlink -f $SOURCEDIR/..

it gives output as : /local/mnt/abcd/sub not /local/mnt/abcd/sub\ 1 Basically, it is not able to handle if there is a space present in the folder name. So I want to know is there any alternative to readlink or any other way we can get the absolute path

chepner
  • 497,756
  • 71
  • 530
  • 681

2 Answers2

2

You need to quote the parameter expansion so that readlink gets the path as a single argument.

readlink -f "$SOURCEDIR/.."

Without the quotes, it's equivalent to

readlink -f /local/mnt/abcd/sub 1/..
chepner
  • 497,756
  • 71
  • 530
  • 681
2

Put double-quotes round the argument to keep it as a single argument:

readlink -f "$SOURCEDIR/.."
Gem Taylor
  • 5,381
  • 1
  • 9
  • 27