5

I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:

/tmp/test/test2/test3/test.sh

path=$PWD
echo $path

Gives /tmp/test/test2/test3. But I want to exclude the folder where the sh script is in.

So my desired output would be: /tmp/test/test2

2
  • Are you talking about the current (working) directory, where the script is running, or the directory where the script is stored? Oct 29, 2018 at 15:48
  • I would use realpath .. (the question is tagged ubuntu, so a linux-only solution is OK)
    – user313992
    Oct 29, 2018 at 17:26

5 Answers 5

8

You may use

dir=${PWD%/*}    # or:  dir=$( dirname "$PWD" )

or

dir="$PWD/.."

or

dir=".."

In the first case, the PWD variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last / and everything after it.

In the second case, the PWD variable is used to simply create a path for the directory above using ...

In the third case, we simply refer to the directory above using a relative path from the current directory.

Which alternative you use is dependent on what you are going to use the path for.

Examples:

$ echo $PWD
/tmp/shell-sh.jAtMYgn0
$ dir=${PWD%/*}
$ echo "$dir"
/tmp
$ dir="$PWD/.."
$ echo "$dir"
/tmp/shell-sh.jAtMYgn0/..
$ dir=".."
$ echo "$dir"
..
2
  • 1
    Note that if $PWD is /, ${PWD%/*} becomes the empty string while dirname / outputs /. In any case, / would probably have to be handled specially. Oct 29, 2018 at 15:26
  • Note that if $PWD is /tmp and /tmp is a symlink to /var/tmp, $PWD/.. refers to /var, not / (except for cd in POSIX-like shells, when not passed the -P option). Oct 29, 2018 at 15:28
4

try the following code,

dirname "$(pwd)"
  • dirname - strip suffix from file/dir path
1
  • leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable. Aug 15, 2018 at 9:00
1

In zsh:

dir=$PWD:h

Like dirname, That removes one directory component off the end of $PWD. When $PWD is /, $dir becomes / like with dirname.

One advantage over

dir=$(dirname -- "$PWD")

is that it still works if the dirname of $PWD ends in newline characters.

Note that it is not necessarily the parent of the current directory. For instance, when $PWD is /tmp and /tmp happens to be a symlink to /var/tmp, the parent directory of the current directory would be /var, not /. To get the parent directory, you could do:

dir=$PWD/..
dir=$dir:P

or:

dir=$PWD:P:h

(the :P modifier makes $dir a canonical path (resolves all symlink, ., and .. components like GNU readlink -f)).

Or POSIXly:

dir=$(cd -P .. && pwd -P)

In any case, note that $PWD within a script has nothing to do with the path of the script. If you want the parent directory of the directory containing the script, that would be $0:P:h:h in zsh.

-1

Finally it was as simple as: current=$(dirname "$(pwd)")

-1

Current working directory ie: /home/dev/other

dir=$PWD

Parent directory

parentdir="$(dirname "$dir")"`
4
  • This answer has been given multiple times already (but without the unnecessary step of copying one variable to another). Oct 29, 2018 at 15:44
  • @G-Man, technically it hadn't until I added my answer (shortly after Piyush's) as the other answers either use ${PWD%/*} or dirname "$(pwd)". Oct 29, 2018 at 15:46
  • @StéphaneChazelas: Well, Kusalananda’s answer says dir=$( dirname "$PWD" ) in a comment, indicating that it is equivalent to his primary answer of dir=${PWD%/*} (although it isn’t).  Also, lgeorget deleted their answer (10K only) of dirname $PWD, possibly because it was considered to be equivalent to msp9011’s answer of dirname "$(pwd)". … (Cont’d) Oct 29, 2018 at 16:05
  • (Cont’d) …  (I hope you aren’t giving Piyush Sharma ‘credit’ for having a new and unique answer just because they copied $PWD into another variable before passing it to dirname.) Oct 29, 2018 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .