0

I obtain the directory of the current file using this code:

dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

I would like to go down one level in the file structure.

For instance if my file is at:

/a/b/c/myFile.sh

dir will be /a/b/c

I would like to go to /a/b

So I am running this command:

containingdir = "$( cd "$dir" && cd ".." && pwd )"

However, I'm getting this error on the line where I defined the command:

myFile.sh: line 13: containingdir: command not found

What's the cause of this error?

bsky
  • 19,326
  • 49
  • 155
  • 270
  • 2
    you are saying `var = "$(command)"`, note the spaces around `=`. This is wrong, since it tries to run the command `var` with parameters `"$(command)"`. Just remove those spaces. – fedorqui Jan 18 '17 at 14:31
  • Perhaps `containingdir=$(readlink -f "${BASH_SOURCE[0]}"/..)` would be sufficient... – twalberg Jan 18 '17 at 16:10

1 Answers1

1

Just use dirname (twice if needed):

reut@reut:~$ dirname $(dirname /a/b/c/myFile.sh)
/a/b
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88