我正在尝试使用为ubuntu用户安装oh-my-zsh引导我的AWS EC2 ubuntu服务器。我有一个cloud-init脚本(在此处了解更多信息),以root用户身份运行(使用sudo)。因此,在我的脚本中,我以ubuntu用户身份运行oh-my-zsh安装。
#cloud-config
runcmd:
# omitted other commands specific to my server, install zsh at the end
- apt-get install -y zsh
- su ubuntu -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/coreycole/oh-my-zsh/master/tools/install.sh)"'
- chsh -s $(which zsh) ubuntu
# change the prompt to include the server hostname
- su ubuntu -c echo "echo export PROMPT=\''%{$fg[green]%}%n@%{$fg[green]%}%m%{$reset_color%} ${ret_status} %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'\'" >> /home/ubuntu/.zshrc
# get environment variables defined above
- echo "source ~/.profile" >> /home/ubuntu/.zshrc
当cloud-init结束并且我在ssh进入颜色不起作用时$PROMPT
,我看到[green]
和[cyan]
:
[green]ubuntu@[green]ip-172-31-27-24 [cyan]~
如果PROMPT
在ssh's后运行与ubuntu用户相同的命令,则颜色可以正常工作:
问题似乎出在cloud-init脚本运行echo
命令时如何评估颜色,而ubuntu用户运行echo
命令时如何评估颜色。有谁知道我将如何更改PROMPT
颜色,以便仅~/.zshrc
在ubuntu用户撤消一次后撤消颜色?
我在这里感谢jgshawkey的回答解决了这个问题。我使用bash变量来转义颜色代码和命令以推迟其评估:
- apt-get install -y zsh
- runuser -l ubuntu -c 'sh -c "$(curl -fsSL https://raw.githubusercontent.com/coreycole/oh-my-zsh/master/tools/install.sh)"'
- chsh -s $(which zsh) ubuntu
- fgGreen='%{$fg[green]%}'
- fgCyan='%{$fg[cyan]%}'
- fgReset='%{$reset_color%}'
- retStatus='${ret_status}'
- gitInfo='$(git_prompt_info)'
- runuser -l ubuntu -c "echo export PROMPT=\''${fgGreen}%n@%m${fgReset} ${retStatus} ${fgCyan}%c${fgReset} ${gitInfo}'\'" >> /home/ubuntu/.zshrc
- echo "source ~/.profile" >> /home/ubuntu/.zshrc
它最终看起来像这样~/.zshrc
:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句