0

My homework question given is:

How can you print the path of the current directory (working directory) and how can you use it as a variable?

The first part of the question is easily answered: By using .

But how can I use the output as a variable?

A-Tech
  • 806
  • 6
  • 22
Timothy
  • 608
  • 3
  • 10
  • 24
  • Have a look at the defined environment variables (hint: use `env` to print them). Or initialize your own variable with the output of `pwd`. – Benjamin Bannier Jul 18 '10 at 19:05

2 Answers2

5

Bash has a variable PWD which should be preferred over the pwd command:

echo "$PWD"
Philipp
  • 48,066
  • 12
  • 84
  • 109
1

In bash, you can execute a command and obtain the output using backticks, for example

paul@paul-laptop:~$ WORKING_DIRECTORY=`pwd`
paul@paul-laptop:~$ echo $WORKING_DIRECTORY
/home/paul

There is an alternate syntax too, using a dollar sign and brackets - WORKING_DIRECTORY=$(pwd)

brabster
  • 42,504
  • 27
  • 146
  • 186