【问题标题】:Modify Bash prompt prefix in OS X terminal [closed]修改 OS X 终端中的 Bash 提示前缀 [关闭]
【发布时间】:2014-12-01 12:00:45
【问题描述】:

我在终端的空间“names-MacBook-Pro”上有一个巨大的提示,在每一行输入命令的同一行。

有没有办法可以删除它或缩短它?

【问题讨论】:

标签: bash macos terminal


【解决方案1】:

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

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

echo $PS1

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

export PS1="$"

通过执行以下操作从您的点文件重新加载设置:

source ~/.bash_profile

这将使您的新 shell 提示符只是一个 $


提示变量

  • PS1 :主要提示字符串。默认值为\s-\v\$ .
  • PS2 :辅助提示字符串。默认为>
  • PS3 :提示select 命令
  • PS4 :在执行跟踪期间 Bash 显示的每个命令之前打印。 PS4 的第一个字符会根据需要重复多次,以指示多个间接级别。默认为+

语法(来自 Bash manual

\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

【讨论】:

  • 我想在提示符和输入的命令之间留一个空格,所以我选择自己加一个空格:export PS1="$ "
  • 大卫,提醒一下:你的答案is being discussed on Meta
  • export PS1="$" 最好是 export PS1="$" 因为这可以帮助您区分命令提示符和您输入的实际命令
  • 我确实导出 PS1="[ \u@\h:\w ]\n$ "
  • 请注意,PS1 对我来说的默认值是 \h:\W \u\$,而这个答案中列出的默认值并没有做任何有用的事情,他们可能从那时起改变了它
猜你喜欢
  • 2010-09-11
  • 2010-12-05
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 2016-11-09
  • 2013-06-04
  • 2013-11-01
相关资源
最近更新 更多