From ArchWiki
Jump to: navigation, search

Return in non interactive mode

[Moved from Talk:Color Bash Prompt#Return in non interactive mode -- Kynikos (talk) 03:18, 26 April 2014 (UTC)]

# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !

# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.

# If not running interactively, don't do anything!
[[ $- != *i* ]] && return

Some Linux distributions like ArchLinux actually distribute a bashrc with a statement like [[ $- != *i* ]] && return included (see above). The comments say the bashrc script has to exit (return) because tools like scp are running in interactive mode and don't support having some user customizations, this is the reason why we have to return. But the return code actually returns if the script isn't run in interactive mode (!= *i*), which is the contrary of what we want to achieve. Can anyone explain me that? Either the comments are incorrect or it is unneeded to use that return statement. But on IRC, some people agreed with me regarding this problem. And on the #bash channel, some said the statement is unneeded because all scripts (which are all running in non-interactive mode) do not source ANY configuration files like /etc/profile and bashrc file. Wget (talk) 20:18, 25 August 2013 (UTC)

Doesn't really belong here. --Dettalk 11:37, 25 April 2014 (UTC)
Certainly, but as the question is not really answered, I'd suggest to move this discussion into Talk:Bash. The snippet is probably from some content recently removed from this page, but this should not matter. -- Lahwaacz (talk) 21:17, 25 April 2014 (UTC)
Agreed, moved here as suggested. -- Kynikos (talk) 03:18, 26 April 2014 (UTC)
From [1]:
Bash has a special compile time option that will cause it to source the .bashrc file on non-login, non-interactive ssh sessions. This feature is only enabled by certain OS vendors (mostly Linux distributions). It is not enabled in a default upstream Bash build, and (empirically) not on OpenBSD either.
[...] Note that a test on SHLVL is also done, so if you do: ssh remotehost bash -c echo then the first bash will source .bashrc but not the second one (the explicit bash on the command line that runs echo).
The behaviour of the bash patched to source a system level bashrc by some vendors is left as an exercise.
(Emphasis mine; Arch sources a system-level bashrc).
From [2]:
When Bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. [...] but the value of the PATH variable is not used to search for the filename.
As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.
The above comment on scp was removed from .bashrc files in Arch Linux. Either way, it is clear that non-interactive shells do source configuration files under some circumstances. Questions:
  1. What is the "special compile time option" for ssh sessions?
  2. If files specified in BASH_ENV do not use PATH, what is then used? A default? The current directory?
  3. Does a non-interactive login shell also source files in BASH_ENV besides .profile & co. ?
-- Alad (talk) 14:21, 16 August 2015 (UTC)