如何在通过命令行运行 /bin/bash 时将 $TERM 设置为一个值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14467809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:20:13  来源:igfitidea点击:

How to set $TERM to a value when running /bin/bash via command line?

bash

提问by Dmitri Shuralyov

When I run the /bin/bashprocess with 2 parameters -cand SomeUserInput,

当我/bin/bash使用 2 个参数-c和运行该过程时SomeUserInput

where SomeUserInput is echo $TERM

SomeUserInput 在哪里 echo $TERM

The output is

输出是

xterm-256color

xterm-256color

Is there a way I can set the value of $TERM via a command line parameter to /bin/bashso the above invokation of echo $TERMwould print something else that I specify?

有没有办法可以通过命令行参数/bin/bash设置 $TERM 的值,以便上面的调用echo $TERM会打印我指定的其他内容?

(Yes, I've done a lot of digging in man bashand searching elsewhere, but couldn't find the answer; although I think it's likely there.)

(是的,我已经man bash在其他地方进行了大量挖掘和搜索,但找不到答案;尽管我认为它很可能在那里。)

回答by that other guy

First of all, since you used double quotes, that prints the value of TERM in your current shell, not the bash you invoke. To do that, use /bin/bash -c 'echo $TERM'.

首先,由于您使用了双引号,因此会在当前 shell 中打印 TERM 的值,而不是您调用的 bash。为此,请使用/bin/bash -c 'echo $TERM'.

To set the value of TERM, you can export TERM=linuxbefore running that command, set it only for that shell with either TERM=linux /bin/bash -c 'echo $TERM'(shell expression), or /usr/bin/env TERM=linux /bin/bash -c 'echo $TERM'(execve compatible (as for find -exec)).

要设置 TERM 的值,您可以export TERM=linux在运行该命令之前,仅使用TERM=linux /bin/bash -c 'echo $TERM'(shell 表达式) 或/usr/bin/env TERM=linux /bin/bash -c 'echo $TERM'(execve compatible (for find -exec))为该 shell 设置它。

Update: As for your edit of only using command line parameters to /bin/bash, you can do that without modifying your input like this:

更新:至于您只使用命令行参数的编辑/bin/bash,您可以这样做而无需像这样修改您的输入:

/bin/bash -c 'TERM=something; eval ""' -- 'SomeUserInput'

回答by Rubens

Well, you can either set the variable on your .bashrcfile, or simply set with the bash invocation:

好吧,您可以在.bashrc文件上设置变量,也可以简单地使用 bash 调用进行设置:

/bin/bash -c "TERM=something-else; echo $TERM"