How can I tell (in ~/.bashrc
) if I'm running in interactive mode, or, say, executing a command over ssh. I want to avoid printing of ANSI escape sequences in .bashrc
if it's the latter.
Bash – How to check in bash if a shell is running in interactive mode
bashshell
Related Topic
- Ssh – Why does bash invocation differ on AIX when using telnet vs ssh
- Bash – pbs/Torque interactive jobs: bash trick to run command after launch
- Linux – How to run command as user who has /usr/sbin/nologin as Shell
- Bash – Run an interactive bash subshell with initial commands without returning to the (“super”) shell immediately
- Linux Bash – Where is $BASH_ENV Usually Set?
- Ssh – shell script for running ssh-agent
Best Answer
According to
man bash
:So you can use:
Also:
So
~/.bashrc
is only sourced for interactive shells. Sometimes, people source it from~/.bash_profile
or~/.profile
which is incorrect since it interferes with the expected behavior. If you want to simplify maintenance of code that is common, you should use a separate file to contain the common code and source it independently from both rc files.It's best if there's no output to stdout from login
rc
files such as~/.bash_profile
or~/.profile
since it can interfere with the proper operation ofrsync
for example.In any case, it's still a good idea to test for interactivity since incorrect configuration may exist.