无法添加转义序列动态地'PS1' [英] Unable to add escape sequences dynamically for 'PS1'

查看:183
本文介绍了无法添加转义序列动态地'PS1'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想包括动态的转义序列:

 如果[-n $东西];然后
    用户=\\ U
其他
    用户=管理员
科幻
PS1 ='$用户@ \\ H $'

问题是,而不是在用户名填写,我的提示是这样的:

  \\ü@ Ubuntu的$ 1

即使我转义反斜线(用户=\\\\ U),它仍然不打印出用户名。我如何得到提示如下:

 安德烈亚斯@ Ubuntu的$ 1


解决方案

当您试图插值变量,并希望他们扩大使用双引号。

您也有另一种选择,而不是使用处理\\ U 和并发症时,它的内部pretation情况与

 如果[-n $东西];然后
    用户=`whoami`
其他
    用户=管理员
科幻
PS1 =$用户名@ \\ H $

Say I want to include an escape sequence dynamically:

if [ -n $something ]; then
    user="\u"
else
    user="admin"
fi
PS1='$user@\h$ '

The problem is, instead of filling in the user name, my prompt looks like this:

\u@ubuntu-1$ 

Even if I escape the backslash (user="\\u") it still does not print out the user name. How do I get the prompt to look like this:

andreas@ubuntu-1$ 

解决方案

Use double quotes when you are trying to interpolate variables and want them to expand.

You also have another option, instead of dealing with \u and complications with when the interpretation of it happens.

if [ -n $something ]; then
    user=`whoami`
else
    user="admin"
fi
PS1="$user@\h$ "

这篇关于无法添加转义序列动态地'PS1'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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