Bash prompt

Giorgos Keramidas keramida at
Sun Jul 17 22:45:11 GMT 2005

On 2005-07-18 00:30, Alex Yarmol <alex.yarmol at> wrote:
> Hi, dear FreeBSD developers etc. :)
> I have a question.
> How I can chage my bash prompt to this:
> [user at host directory-name(e. g. "alex" for /usr/home/alex)]$
> I assume that I need to do that:
> export PS1='[\u@\h \(here i don't know what to do, i assume, that I
> need to write "\p" or "\P", but it's not working)]\$
> I've try to read "man bash", but didn't find what I want :(

The manpage of bash(1) contains a section called ``PROMPTING'' that
explains all the "backslash-starting escape sequences" you can use.

Having said that though, the real ``working directory'' cannot be inlined
in the PS1 shell variable by using an escape.  The closest one can come up
with is something that uses ``\w'', but this is not quite right, because it
changes the HOME directory to ``~'' and there is no way to turn that off
(at least, AFAIK).

A workaround for this is to embed a literal ``${PWD}'' in the value of PS1,
but you have to be careful to AVOID having this expanded only at the time
PS1 is set, by using the proper sort of quote characters.  Something like
this won't work:

	$ export PS1="[\u@\h ${PWD}]\$ "

because the shell is allowed to expand the contents of "..." quoted strings.

The correct way to set PS1 using inline escape sequences and references to
environment variables like ``$PWD'', which may change between subsequent
PS1 references by the shell, is with something that uses single quotes:

	$ export PS1='[\u@\h ${PWD}]\$ '

I hope this helps a bit.

If you need more details, there's a whole HOWTO document describing a lot
more details than any sane person is ever going to need about bash prompts.
You can find it on any mirror of the Linux Documentation Project.  Since
the GNU bash shell runs on both Linux and FreeBSD, most of it applies
verbatim to FreeBSD too.

- Giorgos

More information about the freebsd-questions mailing list