In both bash and zsh, the value of PS1
is not used as a prompt as-is, it undergoes some expansions. The rules differ for the two shells, but in both cases, one of the step is to perform “dollar” expansions (variable substitution, command substitutions, arithmetic evaluation) with the same syntax as in normal shell syntax ($VARIABLE
, ${VARIABLE}
, $(COMMAND)
or `COMMAND`
, $((EXPRESSION))
, $[EXPRESSION]
) .
- In bash, dollar expansion is turned on by default, but can be turned off with
shopt -u promptvars
.
- In zsh, dollar expansion is off by default, but many people (and most configuration frameworks you'll find on the web) turn it on with
setopt prompt_subst
.
With dollar expansion in the prompt turned on, PS1='$(pwd)'
sets PS1
to the 6-character value $(pwd)
and thus causes $(pwd)
to be substituted, and therefore the pwd
command to be executed, each time the shell displays a new prompt. On the other hand, PS1=$(pwd)
sets PS1
to whatever is the shell's current working directory at the time. If you had dollar expansion turned off then PS1='$(pwd)'
would cause the prompt to be the literal string $(pwd)
.
Note that there are more convenient ways to get the working directory in the prompt:
- In bash, with a backslash escape such as
\w
, which abbreviates your home directory to ~
and may be trimmed by setting PROMPT_DIRTRIM
.
- In zsh, with a percent escape such as
%/
or %~
(%/
is the same as $PWD
, %~
abbreviates home directories), which can have a trimming setting.
- In either shell (and any other Bourne-style shell),
$PWD
is equivalent to $(pwd)
: you don't need to run a subprocess to get the current working directory.