Previously we only checked if the new directory was already in the PATH
and, if yes, didn't prepend it. This can cause the new directory to be
in an unexpected position.
Now we first remove any occurrences of the new directory (to prevent
duplication) and prepend it so it's in the position we expect.
Two main changes:
- avoid 2 additional fork() calls by evaluating \j instead of using
$(jobs | wc -l) -> one fork for subshell and another for `wc`.
- make the build up of the prompt a bit more modular by using an
array to collect the pieces and "sort of" join them in the end.
Avoiding starting many processes during creation of the PS1 string makes
the shell much more responsive, especially under Cygwin, which has
problems with fork().
The previous solution worked fine for coloring the man pages but the
display in bash's job control was a disaster:
$ man sshd_config # followed by CTRL-z
$ jobs
[1]+ Stopped env LESS_TERMCAP_so=$(echo
-ne"\033[${standout}m") LESS_TERMCAP_md=$(echo -ne "\033[${bold}m")
LESS_TERMCAP_us=$(echo -ne "\033[${underline}m") LESS_TERMCAP_se=''
LESS_TERMCAP_me='' LESS_TERMCAP_ue='' GROFF_NO_SGR=1 man sshd_config
The new solution behaves identically regarding coloring the man pages
but the job control information is much more useful:
$ man sshd_config # followed by CTRL-z
$ jobs
[1]+ Stopped man sshd_config
Most of these variables are used only within the shell and don't need to
clutter the environment. The notable exception is MANPATH, which we
explicitly want to export.