简体   繁体   English

如何让FreeBSD显示当前shell的完整路径?

[英]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 . FreeBSD中的默认用户shell是Bourne shell /bin/sh You change the prompt by setting the PS1 variable. 您可以通过设置PS1变量来更改提示。 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 . 要在每次登录时自动完成,您应该更改主目录.shrc的配置文件。

The .shrc file already have an alternative prompt you could use - you just need to uncomment these lines: .shrc文件已经有了你可以使用的替代提示 - 你只需要取消注释这些行:

# 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). 由于"\\$"设置$或#(用户/ root)的提示,因此不需要案例结构。

The Bourne Shell is quite powerful and command line editing is readily available in the FreeBSD variant. Bourne Shell非常强大,FreeBSD变体中提供了命令行编辑功能。 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. 请注意,FreeBSD中的Bourne shell比Linux上更强大。 This is in part why bash is predominant on Linux. 这部分是为什么bash在Linux上占主导地位的原因。 The default shell on FreeBSD is more usable out of the box. FreeBSD上的默认shell更易于使用。 If you are used to Linux you can change to bash to feel more at home. 如果你已经习惯了Linux,你可以改为使用bash来感受更多的感觉。 If not - then I would spend the time to learn Bourne on FreeBSD. 如果没有 - 那么我会花时间在FreeBSD上学习Bourne。 If you outgrow that - then I would look at something like "zsh". 如果你长大了 - 那么我会看一下像“zsh”这样的东西。 But if your level is at figuring out "PS1" I would strongly recommend to stick with the defaults! 但如果你的水平是在计算“PS1”,我强烈建议坚持默认值!

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. export PS1='\\u@\\H: \\W $'是一个bash-ism。 That is only applicable if you use the bash shell. 这仅适用于使用bash shell的情况。 FreeBSD Bourne does not have "\\u\u0026quot; . FreeBSD Bourne没有"\\u\u0026quot;
  2. For historic reasons the shell for "root" is set for "csh". 由于历史原因,“root”的shell设置为“csh”。 The csh shell in FreeBSD is the "tcsh" variant. FreeBSD中的csh shell是“tcsh”变种。 It is however only set for root - and you should never log in as root! 但是它只设置为root - 你永远不应该以root身份登录! All users have the Bourne shell as default. 所有用户都默认使用Bourne shell。 I would recommend against using "csh". 我建议不要使用“csh”。 Rather than su to "root" you could do a "su - toor" which is an alternate root account without the csh shell. 你可以做一个“su - toor”而不是su“to root”,这是一个没有csh shell的备用root帐户。 This is important as you should not change the root shell away from csh! 这很重要,因为你不应该改变root shell远离csh!
  3. There is absolutely no reason to change shell just to get a suitable prompt. 绝对没有理由改变shell以获得合适的提示。

Update: 更新:
There are several reasons you should not change the shell of the root user: 有几个原因你不应该更改root用户的shell:

  1. No need! 没必要! The best practice is to never log in as the root user interactively. 最佳做法是永远不要以root用户身份以交互方式登录。 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" . 如果您发现自己以普通用户身份登录并仍希望以交互方式使用root用户 - 您仍然可以使用sudo -ssu root -c "/path/to/shell"以多种方式轻松完成此操作。 Make a good habit of using root permissions rather than the root user . 养成使用root 权限而不是root 用户的好习惯。 Most of the time you should be using sudo and not an interactive root shell. 大多数时候你应该使用sudo而不是交互式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 . 如果您的系统出现问题,您最终只能安装/bin In those cases it is very important that the shell is statically linked and placed in /bin . 在这些情况下,将shell静态链接并放置在/bin非常重要。 Third-party shells are usually placed in /usr/local/bin (and dynamically linked) which might not be mounted in your emergency situation. 第三方shell通常放在/usr/local/bin (并且动态链接)中,这可能不会在您的紧急情况下安装。

  4. You have the toor user for this exact reason . 出于这个原因,您有toor用户。 It has the same uid and gid as root . 它与root具有相同的uidgid You can set the shell to your hearts desire for the toor user and have a clean root account. 您可以将shell设置为您对toor用户的心愿,并拥有一个干净的root帐户。 Simply use su - toor rather than su - (or just create and alias for su ). 简单地使用su - toor而不是su - (或者仅为su创建和别名)。

References: 参考文献:

How to set the PS1 prompt in different shells: http://www.understudy.net/custom.html 如何在不同的shell中设置PS1提示符: http//www.understudy.net/custom.html

Top Ten Reasons not to use the C shell: http://www.grymoire.com/unix/CshTop10.txt 不使用C shell的十大理由: http//www.grymoire.com/unix/CshTop10.txt

Csh Programming Considered Harmful: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/ Csh编程被认为是有害的: 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 Bourne Shell的PS1变量手册页https://www.freebsd.org/cgi/man.cgi?query=sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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