Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

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

like image 711
Dmitri Shuralyov Avatar asked Jan 22 '13 20:01

Dmitri Shuralyov


2 Answers

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'
like image 126
that other guy Avatar answered Oct 12 '22 19:10

that other guy


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"
like image 42
Rubens Avatar answered Oct 12 '22 19:10

Rubens