How can I get the current working directory?

1,040,856

Solution 1

There's no need to do that, it's already in a variable:

$ echo "$PWD"
/home/terdon

The PWD variable is defined by POSIX and will work on all POSIX-compliant shells:

PWD

Set by the shell and by the cd utility. In the shell the value shall be initialized from the environment as follows. If a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory that is no longer than {PATH_MAX} bytes including the terminating null byte, and the value does not contain any components that are dot or dot-dot, then the shell shall set PWD to the value from the environment. Otherwise, if a value for PWD is passed to the shell in the environment when it is executed, the value is an absolute pathname of the current working directory, and the value does not contain any components that are dot or dot-dot, then it is unspecified whether the shell sets PWD to the value from the environment or sets PWD to the pathname that would be output by pwd -P. Otherwise, the sh utility sets PWD to the pathname that would be output by pwd -P. In cases where PWD is set to the value from the environment, the value can contain components that refer to files of type symbolic link. In cases where PWD is set to the pathname that would be output by pwd -P, if there is insufficient permission on the current working directory, or on any parent of that directory, to determine what that pathname would be, the value of PWD is unspecified. Assignments to this variable may be ignored. If an application sets or unsets the value of PWD, the behaviors of the cd and pwd utilities are unspecified.


For the more general answer, the way to save the output of a command in a variable is to enclose the command in $() or ` ` (backticks):

var=$(command)

or

var=`command`

Of the two, the $() is preferred since it is easier to build complex commands like:

command0 "$(command1 "$(command2 "$(command3)")")"

Whose backtick equivalent would look like:

command0 "`command1 \"\`command2 \\\"\\\`command3\\\`\\\"\`\"`"

Solution 2

dir=$(pwd)

This is more portable and preferred over the backticks method.

Using $() allow you to nest the commands

eg : mech_pwd=$(pwd; echo in $(hostname))

Solution 3

You can either use the environment variable $PWD, or write something like:

dir=`pwd`

Solution 4

You need to use command substitution to save output of pwd command to a variable. Command substitution can use backticks or dollar characters. Like this:

$ mkdir "/tmp/new dir"
$ cd "/tmp/new dir"
$ CWD="$(pwd)"
$ echo $CWD
/tmp/new dir
$ cd ~
$ echo $CWD
/tmp/new dir
$ pwd
/home/ja

Solution 5

The value of the current working directory can be different. If you used symbolic links to get the the current directory, pwd will give different results than /usr/bin/pwd. Since you are using bash, I would use:

dir=$(/usr/bin/pwd)

or as per comment:

dir=$(pwd -P)

as I don't like back quotes since they can't nest.

Share:
1,040,856

Related videos on Youtube

terdon
Author by

terdon

Elected moderator on Unix & Linux. I've been using Linux since the late '90s and have gone through a variety of distributions. At one time or another, I've been a user of Mandrake, SuSe, openSuSe, Fedora, RedHat, Ubuntu, Mint, Linux Mint Debian Edition (basically Debian testing but more green) and, for the past few years, Arch. My Linux expertise, such as it is, is mostly on manipulating text and regular expressions since that represents a large chunk of my daily work.

Updated on September 18, 2022

Comments

  • terdon
    terdon over 1 year

    I want to have a script that takes the current working directory to a variable. The section that needs the directory is like this dir = pwd. It just prints pwd how do I get the current working directory into a variable?

    • Admin
      Admin almost 7 years
    • Admin
      Admin about 5 years
      This is not a duplicate of the question for which it is currently marked as one. The two questions should be compared, at least, based on their titles (as well as their answers). That the answer to this question is already covered by another is, or should be, irrelevant.
    • Admin
      Admin about 5 years
      @KennyEvitt actually, one of the main reasons we close is precisely because an answer has been given elsewhere. And, in fact, the main question here is actually how to assign the output of a command to a variable, which is covered by the dupe. I have also given the answer to this specific case, so all bases are covered. There would be no benefit in opening this again.
    • Admin
      Admin about 5 years
      @terdon As a resource available, and intended, for the entire population of Unix & Linux users, this is a valuable question, even if the original asker really just needed an answer already covered elsewhere. If anything, I think this question should be edited to more closely match its title and it should be re-opened, not to allow further activity, but to not imply that this question is 'bad'.
    • Admin
      Admin about 5 years
      @KennyEvitt closing as a duplicate in no way implies that the question is bad! This question will remain here, answered, for ever. If you really want to know how to get the current working directory, you will find your answer here. If you just want to know how to save the output of a command in a variable, you will also find the answer here by following the link to the dupe. In any case, this isn't really something I should do alone, if you feel strongly that it should be reopened, please open a discussion on Unix & Linux Meta where such things should be resolved.
  • terdon
    terdon about 9 years
    Actually, if anything, the backtics are more portable since they predate the $() format. There might conceivably still be some machines running shells that don't support the newer $() notation. While both are defined by POSIX, the $() is preferred today because it is a cleaner syntax and can be nested ($(command1 $(command2))) in a far simpler way than the backticks.
  • mahendra yadav
    mahendra yadav about 9 years
    dir=$(pwd -P) also works.