简体   繁体   中英

How to make FreeBSD show full path of current shell?

无论shell目前处于什么目录,控制台和SSH上的FreeBSD 10.1的默认用户提示始终$ 。如何将其更改为user@machine /full/path/to/current/directory $或类似的完整路径?

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\u0026quot; .
  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM