如何更改 PS1 值。

如何更改 PS1 值。

我以 Linux 2.6.18 機器的 root 身分登入。我將 PS1 的值改為:

export PS1="$PWD> "

它可以工作並將命令提示字元硬編碼為主目錄,後跟>.現在我希望它顯示我現在所在的當前目錄。

我該如何設定這個?是否有文件解釋 PS1 變數如何運作的程式碼?

答案1

嘗試這個

匯出 PS1="[\w]$ "

這裡

w 代表目前工作目錄

欲了解更多信息,請參閱此關聯


編輯:這些是 bash 在 PS1 和 PS2 中理解的特殊字符

\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

答案2

你能確保你真正運作的是bash,而不是某種busybox嗎?

echo $SHELL

應該要給你看外殼。 (我假設/bin/bash)

那你可以看看

ls -l /bin/bash

如果它指向其他地方(連結)或它是實際的可執行文件

答案3

這適用於其他 Linux 機器。我不知道為什麼它在這裡不起作用。

也許是因為在其他機器上你有類似的東西

$ PS1="\$(pwd)> "
/tmp> cd /
/>

相關內容