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?

- 242,166
-
2Related: Getting the source directory of a Bash script from within – kenorb Jul 19 '17 at 18:37
-
2This 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. – Kenny Evitt Mar 02 '19 at 21:49
-
@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. – terdon Mar 02 '19 at 23:52
-
3@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'. – Kenny Evitt Mar 03 '19 at 17:53
-
2@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 [meta] where such things should be resolved. – terdon Mar 03 '19 at 18:36
5 Answers
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\\\`\\\"\`\"`"

- 544,893

- 242,166
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))

- 1,401
-
14Actually, 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. – terdon Mar 04 '15 at 18:55
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

- 25,539
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.

- 20,988

- 415