如果您有效地使用PS1,PS2,PS3,PS4和PROMPT_COMMAND,与Linux Bash Shell的交互将变得非常愉快。 PS代表即时声明。本文将通过简单的示例为您提供有关Linux命令提示符环境变量的快速入门。
1. PS1–默认互动提示
可以如下所示将Linux上的默认交互式提示修改为有用和有用的信息。在以下示例中,默认PS1为“\s-\v\$”,其中显示了外壳名称和版本号。让我们更改此默认行为,以显示用户名,主机名和当前工作目录名称,如下所示。
-bash-3.2$ 导出PS1 ="\ u @ \ h \ w> "
[email protected] ~> cd /etc/mail
[email protected] /etc/mail>
[Note: Prompt changed to "[email protected] current-dir>" format]
在此示例中使用以下PS1代码:
- \ u – Username
- \H – Hostname
- \ w –当前目录的完整路径名。请注意,当您位于主目录中时,它将仅显示〜
- 注意,PS1的值末尾有一个空格。就个人而言,我更喜欢在提示的末尾添加一个空格,以提高可读性。
通过添加导出PS1 =将此设置永久化”\[email protected]\h \ w > ”到.bash_profile(或).bashrc,如下所示。
[email protected] ~> vi ~/.bash_profile (or) [email protected] ~> vi ~/.bashrc [Note: Add 导出PS1 ="\ u @ \ h \ w> " to 上 e of the above files]
在下一篇文章中,我’我会写一些 PS1使用的实际示例 详细。
2. PS2–继续互动提示
可以通过在行的末尾加\将非常长的unix命令分解为多行。多行命令的默认交互式提示是“> “。让我们更改此默认行为以显示“continue->”通过使用PS2环境变量,如下所示。
[email protected] ~> myisamchk --silent --force --fast --update-state \
> --key_buffer_size=512M --sort_buffer_size=512M \
> --read_buffer_size=4M --write_buffer_size=4M \
> /var/lib/mysql/bugs/*.MYI
[Note: This uses the default ">" for continuation prompt]
[email protected] ~> export PS2="继续-> "
[email protected] ~> myisamchk --silent --force --fast --update-state \
continue-> --key_buffer_size=512M --sort_buffer_size=512M \
continue-> --read_buffer_size=4M --write_buffer_size=4M \
continue-> /var/lib/mysql/bugs/*.MYI
[Note: This uses the modified "继续-> " for continuation prompt]
当我使用\将长命令分成多行时,我发现它非常有用并且易于阅读。我也见过其他人’我喜欢打乱长命令。您的喜好是什么?您喜欢将长命令分成多行吗?
3. PS3– Prompt used 通过 “select” inside shell script
您可以使用PS3环境变量在shell脚本中为select循环定义自定义提示,如下所述。
Shell脚本和输出而没有PS3:
[email protected] ~> cat ps3.sh
select i in mon tue wed exit
do
case $i in
mon) echo "Monday";;
tue) echo "Tuesday";;
wed) echo "Wednesday";;
exit) exit;;
esac
done
[email protected] ~> ./ps3.sh
1) mon
2) tue
3) wed
4) exit
#? 1
Monday
#? 4
[Note: This displays the default "#?" for 选择 command prompt]
Shell脚本和PS3输出:
[email protected] ~> cat ps3.sh
PS3="Select a day (1-4): "
select i in mon tue wed exit
do
case $i in
mon) echo "Monday";;
tue) echo "Tuesday";;
wed) echo "Wednesday";;
exit) exit;;
esac
done
[email protected] ~> ./ps3.sh
1) mon
2) tue
3) wed
4) exit
Select a day (1-4): 1
Monday
Select a day (1-4): 4
[Note: This displays the modified "Select a day (1-4): "
for 选择 command prompt]
4. PS4– Used 通过 “set -x”前缀跟踪输出
PS4 Shell变量定义在调试模式下执行Shell脚本时显示的提示,如下所示。
Shell脚本和无PS4输出:
[email protected] ~> cat ps4.sh
set -x
echo "PS4 demo script"
ls -l /etc/ | wc -l
du -sh ~
[email protected] ~> ./ps4.sh
++ echo 'PS4 demo script'
PS4 demo script
++ ls -l /etc/
++ wc -l
243
++ du -sh /home/ramesh
48K /home/ramesh
[Note: This displays the default "++" while tracing the output using 设置-x]
Shell脚本和PS4输出:
以下在ps4.sh中定义的PS4具有以下两个代码:
- $0 –指示脚本名称
- $ LINENO–显示脚本中的当前行号
[email protected] ~> cat ps4.sh
export PS4='$0.$LINENO+ '
set -x
echo "PS4 demo script"
ls -l /etc/ | wc -l
du -sh ~
[email protected] ~> ./ps4.sh
../ps4.sh.3+ echo 'PS4 demo script'
PS4 demo script
../ps4.sh.4+ ls -l /etc/
../ps4.sh.4+ wc -l
243
../ps4.sh.5+ du -sh /home/ramesh
48K /home/ramesh
[Note: This displays the modified "{script-name}.{line-number}+"
while tracing the output using 设置-x]
5. PROMPT_COMMAND
Bash shell在显示PS1变量之前执行PROMPT_COMMAND的内容。
[email protected] ~> export PROMPT_COMMAND="date +%k:%m:%S" 22:08:42 [email protected] ~> [Note: This displays the PROMPT_COMMAND and PS1 output 上 different lines]
如果要在与PS1相同的行中显示PROMPT_COMMAND的值,请使用echo -n,如下所示。
[email protected] ~> export PROMPT_COMMAND="echo -n [$(date +%k:%m:%S)]"
[22:08:51][email protected] ~>
[Note: This displays the PROMPT_COMMAND and PS1 output 上 the same line]
如果您喜欢这篇文章,请在其上添加书签 del.icio.us和绊倒 请使用“下一步”部分下面提供的链接。
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
拉梅什
一个无关的问题,但特定于Ubuntu… I heard that there’一个可用于Ubuntu的目录副本安装。几年前我上一次尝试正常安装时,这很可怕。有关此简单安装过程的任何想法。
也许您也可以发表有关此的文章。
干杯,
阿吉斯
PS:因此,我还不是Linux用户