How to insert an environment variable inside the bash prompt

人走茶凉 提交于 2019-12-01 02:13:26

you need to add backslash to get it evaluated not in the time of FOO assigment but during evaluating the PS1, so do:

export PS1="[\$FOO]$ "

instead of:

export PS1="[$FOO]$ "

Note the \ before the $FOO.

Try setting the PROMPT_COMMAND variable:

prompt() {
    PS1="[$FOO]$ "
}

PROMPT_COMMAND=prompt

From http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x264.html:

Bash provides an environment variable called PROMPT_COMMAND. The contents of this variable are executed as a regular Bash command just before Bash displays a prompt.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!