如何插入bash提示符内的环境变量 [英] How to insert an environment variable inside the bash prompt

查看:138
本文介绍了如何插入bash提示符内的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样设置bash提示符内的环境变量:

I can set an environment variable inside the bash prompt like this:

export PS1="[\u@\H/$FOO \W]\$ "

当我改变环境变量的提示并没有改变: $ FOO 因为 $ FOO 变量不除preTED。

The prompt does not change when I change the environment variable: $FOO because the $FOO variable is not interpreted.

我可以通过执行以下,再出口PS1解决它。不过,我想是能够做到这一点在同一行:

I can work around it by doing the following, exporting PS1 again. But I would like to be able to do it on one line:

[user@server ]$ echo $FOO
foo
[user@server ]$ export PS1="[$FOO]$ "
[foo]$ export FOO=bla
[bla]$ 

这能在一行中做了什么?

Can this be done in one line?

推荐答案

您需要添加反斜线得到它在FOO assigment的时间,但在评估过程中PS1评估没有,所以做的:

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]$ "

而不是:

export PS1="[$FOO]$ "

请注意在 \\ $ FOO

这篇关于如何插入bash提示符内的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆