4

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?

3
  • Does export PS1='\u@\H: \W $' work? Commented Mar 14, 2015 at 3:29
  • @ElliottFrisch because FreeBSD uses tcsh as default shell, probably set prompt = '[%n@%m:%~]%# '
    – clt60
    Commented Mar 14, 2015 at 8: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
    Commented Mar 16, 2015 at 11:48

1 Answer 1

5

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:

  1. 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".
  2. 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!
  3. 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:

  1. 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 or su 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 using sudo and not an interactive root shell.

  2. Predictability. You might not be the only admin. Or you might suffer pain.

  3. 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.

  4. You have the toor user for this exact reason. It has the same uid and gid as root. You can set the shell to your hearts desire for the toor user and have a clean root account. Simply use su - toor rather than su - (or just create and alias for su).

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

8
  • Could you please expand on why it is not a good idea to change the root shell away from csh, please? Commented Jul 10, 2016 at 0: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. Commented Jul 11, 2016 at 8: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. Commented Jul 11, 2016 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. Commented Jul 12, 2016 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? Commented Jul 12, 2016 at 21:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.