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 (
- 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
With dollar expansion in the prompt turned on,
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 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
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
- In zsh, with a percent escape such as
%/ is the same as
%~ 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.