The default user prompt for FreeBSD 10.1 on the console and SSH is always $
, no matter what directory the shell is currently in. How do I change this to user@machine /full/path/to/current/directory $
or similar with the full path?

- 484
- 1
- 5
- 10
-
Does `export PS1='\u@\H: \W $'` work? – Elliott Frisch Mar 14 '15 at 03:29
-
@ElliottFrisch because FreeBSD uses `tcsh` as default shell, probably `set prompt = '[%n@%m:%~]%# '` – clt60 Mar 14 '15 at 08:59
-
actually $ does not look like tcsh more like /bin/sh. So i would suggest to use another shell like tcsh (comment2), bash (comment1) or zsh – arved Mar 16 '15 at 11:48
1 Answers
The default user shell in FreeBSD is the Bourne shell /bin/sh
. You change the prompt by setting the PS1 variable. Do this on the command line:
export PS1="`whoami`@\H:\w\$ "
To have it done automatically at every login you should change the configuration file in your home directory .shrc
.
The .shrc
file already have an alternative prompt you could use - you just need to uncomment these lines:
# set prompt: ``username@hostname$ ''
PS1="`whoami`@`hostname | sed 's/\..*//'`"
case `id -u` in
0) PS1="${PS1}# ";;
*) PS1="${PS1}$ ";;
esac
If you want to have the directory as well you can do the same as me. Replace all of the lines above with:
PS1="`whoami`@\H:\w\$ "
The case structure is not needed because of "\$"
which sets the prompt for $ or # (user/root).
The Bourne Shell is quite powerful and command line editing is readily available in the FreeBSD variant. I would recommend you to stick with it as any script you may write will be highly portable. Be aware that the Bourne shell in FreeBSD is more capable than on Linux. This is in part why bash is predominant on Linux. The default shell on FreeBSD is more usable out of the box. If you are used to Linux you can change to bash to feel more at home. If not - then I would spend the time to learn Bourne on FreeBSD. If you outgrow that - then I would look at something like "zsh". But if your level is at figuring out "PS1" I would strongly recommend to stick with the defaults!
There are a couple of comments to your question which I feel is bad advice:
export PS1='\u@\H: \W $'
is a bash-ism. That is only applicable if you use the bash shell. FreeBSD Bourne does not have"\u"
.- For historic reasons the shell for "root" is set for "csh". The csh shell in FreeBSD is the "tcsh" variant. It is however only set for root - and you should never log in as root! All users have the Bourne shell as default. I would recommend against using "csh". Rather than su to "root" you could do a "su - toor" which is an alternate root account without the csh shell. This is important as you should not change the root shell away from csh!
- There is absolutely no reason to change shell just to get a suitable prompt.
Update:
There are several reasons you should not change the shell of the root user:
No need! The best practice is to never log in as the root user interactively. If you do - you are doing it wrong. If you find yourself logging in as a regular user and still wants to use the root user interactively - you can still do that easily in several ways using
sudo -s
orsu root -c "/path/to/shell"
. Make a good habit of using root permissions rather than the root user. Most of the time you should be usingsudo
and not an interactive root shell.Predictability. You might not be the only admin. Or you might suffer pain.
Single user mode. If your system is having problems you can end up having only mounted
/bin
. In those cases it is very important that the shell is statically linked and placed in/bin
. Third-party shells are usually placed in/usr/local/bin
(and dynamically linked) which might not be mounted in your emergency situation.You have the
toor
user for this exact reason. It has the sameuid
andgid
asroot
. You can set the shell to your hearts desire for thetoor
user and have a cleanroot
account. Simply usesu - toor
rather thansu -
(or just create and alias forsu
).
References:
How to set the PS1 prompt in different shells: http://www.understudy.net/custom.html
Top Ten Reasons not to use the C shell: http://www.grymoire.com/unix/CshTop10.txt
Csh Programming Considered Harmful: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/
man page with PS1 variable for the Bourne Shell https://www.freebsd.org/cgi/man.cgi?query=sh

- 1
- 1

- 195
- 4
- 13
-
Could you please expand on why it is not a good idea to change the root shell away from csh, please? – Sopalajo de Arrierez Jul 10 '16 at 00:29
-
The practical reason is that you need to ensure that your root shell is available in emergencies. But most of all the need to change shell shows that you have a bad working pattern. Doing things right is not harder - just a question of habit. – Claus Andersen Jul 11 '16 at 08:48
-
I don`t agree: there could be many reasons to change shell apart from a bad working pattern. Example: I have several Bash scripts that come from Linux and work fine; recycle them would be great. – Sopalajo de Arrierez Jul 11 '16 at 20:18
-
You do not need to change the shell for the root user to be able to use those scripts. Perfect example of a bad reason to change the root shell. – Claus Andersen Jul 12 '16 at 13:48
-
But if a shell script is tested running OK on Bash, there could be many shell expansions that are only guaranteed to work on Bash, not any other shell. What happen if my script do need just a `tail /var/log/gateways.log`, that requires root permissions? – Sopalajo de Arrierez Jul 12 '16 at 21:50
-
You are mixing several different concepts together: 1) Setting the root login shell. 2) Using root interactively 3) Privilege escalation 4) Running interactively and as a script You suggest #1 to be able to do #2 but #3 is more than sufficient for your purpose. These are good and tried principles for all Unix systems - not just FreeBSD. The same apply for Linux. As bash is default you'd naturally use another shell such as zsh for the argument. – Claus Andersen Jul 14 '16 at 07:33
-
See [Why do I have to use sudo for almost everything?](http://unix.stackexchange.com/questions/237221/why-do-i-have-to-use-sudo-for-almost-everything) The [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) `#!` instructs the system which interpreter to use. Another bit of [advice on how to make your bash script OS agnostic](http://stackoverflow.com/questions/17614643/how-do-i-get-a-bash-script-working-on-freebsd-openbsd-and-linux-without-modifyi). – Claus Andersen Jul 14 '16 at 07:36
-
But I digress. These comments do not add any further value to the question on how to change the shell prompt. If you have other doubts you should ask them in form of a new question. – Claus Andersen Jul 14 '16 at 07:36