Arch Linux

Please read this before reporting a bug:
https://wiki.archlinux.org/title/Bug_reporting_guidelines

Do NOT report bugs when a package is just outdated, or it is in the AUR. Use the 'flag out of date' link on the package page, or the Mailing List.

REPEAT: Do NOT report bugs for outdated packages!
Tasklist

FS#46450 - [filesystem] /etc/profile invokes /etc/bash.bashrc even when bash is invoked as sh

Attached to Project: Arch Linux
Opened by Celti Burroughs (Celti) - Sunday, 27 September 2015, 00:39 GMT
Last edited by Sébastien Luttringer (seblu) - Wednesday, 30 September 2015, 19:19 GMT
Task Type Bug Report
Category Packages: Core
Status Closed
Assigned To Dave Reisner (falconindy)
Sébastien Luttringer (seblu)
Architecture All
Severity Low
Priority Normal
Reported Version
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No

Details

/etc/profile contains the following stanza:

# Source global bash config
if test "$PS1" && test "$BASH" && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi

$BASH is set to /usr/bin/sh when /bin/bash is invoked as /bin/sh (as when, e.g., invoked by machinectl shell). /etc/bash.bashrc can contain bash-specific code that will fail when bash is invoked as sh.

Additional info:
* filesystem 2015.02-1

Steps to reproduce:
* Enter bash-specific code in /etc/bash.bashrc — e.g., PS1="\u@\h \w \$([[ \$? != 0 ]] && echo \":( \")\$ "
* Invoke /bin/bash as /bin/sh in such a way that /etc/profile is read — e.g., `machinectl shell` or `chsh -s /bin/sh && sudo -i -u $USER`
* Watch the bash-only PS1 spew errors every time the prompt is rendered.

Steps to fix:
* Change the stanza in /etc/profile as follows:

# Source global bash config
if test "$PS1" && test "$BASH" = "/usr/bin/bash" && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi
This task depends upon

Closed by  Sébastien Luttringer (seblu)
Wednesday, 30 September 2015, 19:19 GMT
Reason for closing:  Fixed
Additional comments about closing:  2015.09-1
Comment by Celti Burroughs (Celti) - Sunday, 27 September 2015, 00:44 GMT
Actually, it's even more complicated to fix than that, because $BASH may be /usr/bin/bash or just /bin/bash. Hmmm.
Comment by Celti Burroughs (Celti) - Sunday, 27 September 2015, 03:15 GMT
A working (slightly ugly) fix should be to replace the test with:

test "$(basename ${BASH}x)" = "bashx"

This fails to source bash.bashrc when bash is invoked as sh in all circumstances I can find, and also works (fails to source) under dash, with no errors in any case. It likewise does work whenever bash is invoked as bash, whether /usr/bin/bash, /bin/bash, or even /home/user/.local/bin/bash.
Comment by Celti Burroughs (Celti) - Sunday, 27 September 2015, 03:19 GMT
An alternative test just suggested to me, less ugly, is: test $BASH && test -z $POSIXLY_CORRECT, making the full stanza:

if test "$PS1" && test "$BASH" && test -z "$POSIXLY_CORRECT" && test -r /etc/bash; then
. /etc/bash.bashrc
fi
Comment by Sébastien Luttringer (seblu) - Wednesday, 30 September 2015, 18:39 GMT
POSIXLY_CORRECT
If this variable is in the environment when bash starts, the shell enters posix mode
before reading the startup files, as if the --posix invocation option had been supplied.
If it is set while the shell is running, bash enables posix mode, as if the command set
-o posix had been executed.

Your test will not works when an empty POSIXLY_CORRECT variable is set.


Something like this looks better:
if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then

Loading...