You can display the value of the present working directory using command substitution in the shell by executing:
bash
echo $(pwd)
This uses command substitution $(...)
to run the pwd
command, which prints
the current working directory, and then echo
displays that output on the
terminal
. Alternatively, you can assign the current directory to a variable using command substitution like this:
bash
current_dir=$(pwd)
echo $current_dir
This stores the output of pwd
in the variable current_dir
and then prints
it
. Both backticks pwd
and the preferred modern syntax $(pwd)
can be used
for command substitution, but $(...)
is recommended for readability and
nesting
. In summary:
- Use
echo $(pwd)
to display the current directory directly. - Use
var=$(pwd)
to capture it into a variable for further use.
These methods are standard in Bash and other POSIX-compliant shells.