bashvariablespwd

How can I use the output from pwd as a variable?


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?


Solution

  • 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)