我想以一种非常具体的方式自定义我的 bash shell,具体到我不知道是否可行。目前,我的外壳是这样的:
myname@ubuntu /home/myname:
>>
其中 myname 是我的用户名。我在以下行中自定义了外壳~/.bashrc
:
PS1='${debian_chroot:+($debian_chroot)}\u@\h `pwd`:\n>> '
当我按 Enter 键时,会发生以下情况:
myname@ubuntu /home/myname:
>>
myname@ubuntu /home/myname:
>>
取而代之的是,我希望发生以下情况:
myname@ubuntu /home/myname:
>>
>>
此外,如果我输入一个命令,会发生什么应该是这样的:
myname@ubuntu /home/myname:
>> echo hello
hello
myname@ubuntu /home/myname:
>>
以下情况不应发生
myname@ubuntu /home/myname:
>>echo hello
hello
>>
这可能吗?如果是这样,该怎么做?
更新
多亏了 ChrisAga 的回应,我才得以实现我的目标。
这是脚本
# don't put duplicate lines or lines starting with space in the history.
HISTCONTROL=ignoreboth
customprompt() {
# the current number of lines in bash history:
bash_history_size=$(fc -l -1)
bash_history_size=${bash_history_size%%[^0-9]*}
# set an initial value to the number of lines
# in bash history stored from the last time
# this function was executed. This avoids bugs
# when running the first command in the current
# shell session
if [ -n "$bash_history_lastsize" ]; then
bash_history_lastsize=0
fi
# if the current number of lines in bash history
# is different from the last number of lines, then
# we print the user name and the current directory.
# otherwise, we just print >>
if [ "${bash_history_size}" != "${bash_history_lastsize}" ]; then
PS1='\[\033[01;32m\]\u@\h \[\033[00m\]`pwd`:\n>> '
else
PS1=">> "
fi
# update the last value to the current value
bash_history_lastsize=${bash_history_size}
}
PROMPT_COMMAND=customprompt