我有.bashrc文件,并在文件中出现这一行:
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
我试图用这个代替它:
PS1='\[\e[32m\]\A\[\e[m\] \[\e[31m\]\u\[\e[m\]@\[\e[36m\]\h\[\e[m\]\[\e[32m\]:\[\e[m\]\[\e[32m\]\w\[\e[m\]\\$ '
我试着这样做:
sed "s#PS1='\$\{debian_chroot:\+\(\$debian_chroot\)\}\\\[\\033\[01;32m\\\]\\u@\\h\\\[\\033\[00m\\\]:\\\[\\033\[01;34m\\\]\\w\\\[\\033\[00m\\\]\\\$ '#PS1='\\\[\\e[32m\\\]\\A\\\[\\e\[m\\\] \\\[\\e\[31m\\\]\\u\\\[\\e\[m\\\]@\\\[\\e\[36m\\\]\\h\\\[\\e\[m\\\]\\\[\\e\[32m\\\]:\\\[\\e\[m\\\]\\\[\\e\[32m\\\]\\w\\\[\\e\[m\\\]\\\\\$ '#g" .bashrc
但我得错误说:
sed: -e expression #1, char 267: Invalid content \{\}
我使用sed或任何其他bash / dash脚本编写方式,因此我可以为我经常使用的系统制作自定义脚本。
感谢您的帮助。
答案 0 :(得分:2)
我会做什么而不是奇怪的ANSI代码:
PURPLE=$(tput setaf 5)
RED=$(tput setaf 1)
WHITE=$(tput setaf 7)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 4)
LIGHT_CYAN=$(tput setaf 6)
STOP=$(tput sgr0)
PS1="\[$PURPLE\]\u\[$WHITE\]@\[$GREEN\]\h\[$WHITE\]:\[$GREEN\]\w\[$WHITE\] $ \[$STOP\]"
最后要对问题的所有方面做出完整的回复:
sed -i '/^PS1=/d' ~/.bashrc # remove PS1 line in bashrc
# now feeding bashrc with goodies :
cat<<'EOF'>>~/.bashrc
PURPLE=$(tput setaf 5)
RED=$(tput setaf 1)
WHITE=$(tput setaf 7)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CYAN=$(tput setaf 4)
LIGHT_CYAN=$(tput setaf 6)
STOP=$(tput sgr0)
PS1="\[$PURPLE\]\u\[$WHITE\]@\[$GREEN\]\h\[$WHITE\]:\[$GREEN\]\w\[$WHITE\] $ \[$STOP\]"
EOF
答案 1 :(得分:0)
这可能适合你(GNU sed):
sed 's|PS1='\''${debian_chroot:+($debian_chroot)}\\\[\\033\[01;32m\\\]\\u@\\h\\\[\\033\[00m\\\]:\\\[\\033\[01;34m\\\]\\w\\\[\\033\[00m\\\]\\$ '\''|PS1='^''\\[\\e[32m\\]\\A\[\\e[m\\] \\[\\e[31m\\]\\u\\[\\e[m\\]@\\[\\e[36m\\]\\h\\[\\e[m\\]\\[\\e[32m\\]:\\[\\e[m\\]\\[\\e[32m\\]\\w\\[\\e[m\\]\\\\$ '\''|' file
仅在模式和替换中将'
替换为'\''
,将\
替换为\\
,并仅在模式中将[]
替换为\[\]
。