Dmitri Shuralyov
Dmitri Shuralyov

Reputation: 1042

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

When I run the /bin/bash process with 2 parameters -c and SomeUserInput,

where SomeUserInput is echo $TERM

The output is

xterm-256color

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

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

Upvotes: 5

Views: 21303

Answers (2)

that other guy
that other guy

Reputation: 123470

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'.

To set the value of TERM, you can export TERM=linux before 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)).

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 -c 'TERM=something; eval "$1"' -- 'SomeUserInput'

Upvotes: 7

Rubens
Rubens

Reputation: 14768

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

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

Upvotes: 2

Related Questions