5

I have a customized prompt with colors (using tput) and every time I start a non-interactive session in the server I get a bunch of errors.
For instance if I start a non-interactive session like this:

ssh root@hostname6 "echo 'hello' ; echo $TERM"

The output I get is:

hello
xterm
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
tput: No value for $TERM and no -T specified
stdin: is not a tty

So the $TERM variable has a value even when the session is non-interactive.

What condition do I have to detect so that when I start a non-interactive shell the prompt customization part is omited??

  • 1
    You need to show how you're customizing your prompt (and in which file). Because it's not supposed to do that. – Dennis Williamson Dec 23 '09 at 18:39
  • I set the variable PS1 in the the file /etc/bashrc – GetFree Dec 23 '09 at 18:44
  • Set it to what? – Dennis Williamson Dec 23 '09 at 19:38
  • Also, are you sure it's /etc/bashrc and not /etc/bash.bashrc or ~/.bashrc? – Dennis Williamson Dec 23 '09 at 19:46
  • It's basically "\u@\h \w" but with colors i.e. with several tput setb 2 (and other colors) in between. And, yes, I'm sure it's /etc/bashrc. I just type "vi /etc/bashrc" to edit it. – GetFree Dec 23 '09 at 20:09
1

The tput commands are evaluated at the time that the assignment to PS1 is made. Since the startup files are processed when an ssh session is started, the assignment is made even though your session is not interactive. You can test for that and only make your assignment when you are actually starting an interactive session.

if [[ $- =~ i ]]
then
    # set PS1 using tput
else
    # set a plain PS1 (or use hard-coded escape sequences)
fi
5

There's a bash built-in test for TTY. I forget when it was added, 3.0? I believe it's relatively new. I use it in scripts where I need different behavior when it's run from cron or a user runs it directly.

if [ -t 0 ]; then
   echo "I'm a TTY"
fi
3

Put the following at the beginning of /etc/bashrc

[ -z "$PS1" ] && return
  • 1
    +1 this seems to be the technique many distributions use in the stock bashrc – freiheit Dec 24 '09 at 3:02
  • How is this supposed to work? The PS1 variable doesn't exist when the shell is not interactive? – GetFree Dec 26 '09 at 0:41
  • Yes. It is set by default for interactive shells and it is not for non-interactive ones. – cstamas Dec 26 '09 at 1:05
1

Here is a description of all 3 methods of doing this:
http://tldp.org/LDP/abs/html/intandnonint.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.