在OS X终端中修改Bash提示符前缀

50
我在终端上输入命令时,每行都会显示一个占用大量空间的提示符 "names-MacBook-Pro"。有没有办法去掉或缩短它?

请参考以下链接:https://dev59.com/Y2Yq5IYBdhLWcg3whQ80 - Biffen
打开系统偏好设置 -> 共享,更改本地主机名。 - Parag Bafna
重复的问题:https://dev59.com/Y2Yq5IYBdhLWcg3whQ80 - the Tin Man
1个回答

90

您的提示符是由环境变量PS1设置的。它是由系统在/private/etc/bashrc中设置的,但通常由用户在其主目录中的点文件中进行修改。

使用此命令检查当前设置:

echo $PS1

通过在您的~/.bash_profile(或先前定义它的任何位置)中设置变量来修改它:

export PS1="$"

通过执行以下操作从您的dotfiles中重新加载设置:

source ~/.bash_profile

这将使你的新shell提示符变成一个简单的$

提示符变量

  • PS1 : 主提示符字符串。默认值为\s-\v\$
  • PS2 : 次要提示符字符串。默认为>
  • PS3 : select命令的提示信息
  • PS4 : 在执行跟踪期间,每个Bash显示的命令前打印。在需要表示多层间接引用时,将复制PS4的第一个字符。默认值为+

Syntax (来自Bash 手册)

\a : An ASCII bell character (07)
\d : The date in “Weekday Month Date” format (e.g., “Tue May 26”)
\D{format} : the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required
\e : An ASCII escape character (033)
\h : The hostname up to the first ‘.’
\H : The hostname
\j : The number of jobs currently managed by the shell
\l : The basename of the shell's terminal device name
\n : Newline
\r : Carriage return
\s : The name of the shell, the basename of $0 (the portion following the final slash)
\t : The current time in 24-hour HH:MM:SS format
\T : The current time in 12-hour HH:MM:SS format
\@ : The current time in 12-hour am/pm format
\A : The current time in 24-hour HH:MM format
\u : The username of the current user
\v : The version of Bash (e.g., 2.00)
\V : The release of Bash, version + patch level (e.g., 2.00.0)
\w : The current working directory, with $HOME abbreviated with a tilde
\W : The basename of the current working directory, with $HOME abbreviated with a tilde
\! : The history number of this command
\# : The command number of this command
\$ : If the effective UID is 0, a #, otherwise a $
\nnn : the character corresponding to the octal number nnn
\\ : A backslash
\[ : Begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
\] : end a sequence of non-printing characters

1
我想在提示符和我输入的命令之间保留一个空格,所以我选择自己添加一个空格:export PS1="$ " - Taylor D. Edmiston
David,提前告诉你一声:你的回答正在Meta上被讨论。链接为https://meta.stackoverflow.com/questions/354094/edit-original-answer-from-another-users-answer。 - Cody Gray
1
导出 PS1="$" 最好是导出 PS1="$ ",因为这有助于区分命令提示符和实际输入的命令。 - Wael Assaf
我执行导出命令 PS1="[ \u@\h:\w ]\n$ " - Deepak Vilakkat
1
只是一个提醒,对于我来说,“PS1”的默认值是“\h:\W \u$”,而此答案中列出的默认值并没有起到任何有用的作用,可能自那时以来已经更改了。 - SubJunk
幸运的是,这些变量可以像其他变量一样读取,例如 echo $PS1declareset... - Sinjai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接