经过几次挫折之后,我决定分享它,以便下次我不用参考我的笔记本,而是用谷歌搜索。
是否有一些关于 PS1 字符串接受值的权威文档?
- 逃逸信件
- 颜色
- Unicode 字符
答案1
- https://man7.org/linux/man-pages/man1/bash.1.html#PROMPTING。只有一件事:这意味着每种颜色都应该以 开始
\[
并以 结束\]
。
\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
- 颜色
txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m' # Black - Background
bakred='\e[41m' # Red
badgrn='\e[42m' # Green
bakylw='\e[43m' # Yellow
bakblu='\e[44m' # Blue
bakpur='\e[45m' # Purple
bakcyn='\e[46m' # Cyan
bakwht='\e[47m' # White
txtrst='\e[0m' # Text Reset
- Unicode 字符,我的发现:使用
\x+hex_value
,我们可以将 Unicode 字符添加到 PS1 字符串中。如何找出任何字符/字符串的 Unicode?使用echo <char> | hexdump -C
。arg 可以是字符串。末尾的 代表一个新行。因为 bash 在行缓冲区的基础上工作;必须刷新才能知道这个字符串是否终止。如果要在提示字符串中插入任何字符,请删除除最后一个字符之外的0a
所有字符(如果需要)。0a
例如:
echo 大 | hexdump -C
00000000 e5 a4 a7 0a |....|
00000004
因此,您可以像这样在 ps 中插入字符:
export PS1='\xe5\xa4\xa7'
我发现,使用转换后的中文字符串,echo -e
和printf
都可以正常工作,输出中文字符,但在 中$PS1
失败。因此,我不得不将命令echo -e string
或 的值赋给一个变量printf string
。
sentence=$(echo -e \xe5\xa4\xa7)
export PS1=$sentence
效果很好。