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#66499 - [filesystem] Running bash in POSIX mode sources /etc/bash.bashrc

Attached to Project: Arch Linux
Opened by Stéphane Gleizes (psydev) - Friday, 01 May 2020, 19:12 GMT
Last edited by Sébastien Luttringer (seblu) - Tuesday, 19 May 2020, 22:33 GMT
Task Type Bug Report
Category Packages: Core
Status Closed
Assigned To 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

Description:

The current check in the default /etc/profile assumes that POSIXLY_CORRECT is set by bash when running in compatibility mode:
```
# Source global bash config
if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi
```

However, according to the bash manual (https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Bash-POSIX-Mode):
> When invoked as sh, Bash enters POSIX mode after reading the startup files.
Which means that POSIXLY_CORRECT is not set (yet), therefore running `sh -l` will source /etc/bash.bashrc.

Fix:

The following patch should do the trick:
32c32
< if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then
---
> if test "$PS1" && test "${BASH##*/}" == bash && test -r /etc/bash.bashrc; then

Additional info:

filesystem package version: 2019.10-2

Steps to reproduce:

Run `sh -l`.
The observed prompt style (PS1) is the one set in /etc/bash.bashrc, instead of the expected default `\s-\v\$`.
Closed by  Sébastien Luttringer (seblu)
Tuesday, 19 May 2020, 22:33 GMT
Reason for closing:  Fixed
Additional comments about closing:  filesystem 2020.05.03-1
Comment by Sébastien Luttringer (seblu) - Saturday, 02 May 2020, 09:34 GMT
Hello,

When invoked as "sh -l" bash, for compatibility reasons old sh implementations, always read /etc/profile. Startup files are read with bashism allowed, and then fall back into posix mode.

When invoked as "bash --posix -l", bash follows posix spec more closely and only load startup file in $ENV and load them in posix mode enabled (no bashism mode).

The test $POSIXLY_CORRECT is here to prevent loading bash.bashrc, which is expected to be a file with bashism. I don't think removing it is a good idea. For example, "ENV=/etc/profile bash --posix", would read this file and bashism will fail. But, it doesn't fix  FS#46450  for the reason you pointed.

I guess bash dev introduced this subtle difference, of reading startup files in posix mode disabled when invoked as sh, for a reason. Maybe they want us to be able to parse our bash files at some point.

Loading in /etc/profile of /etc/bash.bashrc is a hacky way to get interactive login shells loads non-login shell startup files. bash invoked as a non login shell "sh" doesn't read /etc/bash.bashrc, so it makes sense to not load this file when started as a login shell.

I think we could _not_ rely on $BASH content, to know if we were invoked as sh. There is no guaranty that the bash binary even contains the bash string. We test this vars to assume we are running into bash.
Instead, I suggest instead to use $0, like in the following:

-# Source global bash config
-if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then
+# Source global bash config, when interactive but not posix or sh mode
+if test "$BASH" -a "$PS1" -a -z "$POSIXLY_CORRECT" -a "${0#-}" != sh -a -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi

I'm testing it locally.
Comment by Sébastien Luttringer (seblu) - Saturday, 02 May 2020, 10:30 GMT
filesystem 2020.05-1 will land in testing soon.
Comment by Stéphane Gleizes (psydev) - Saturday, 02 May 2020, 10:33 GMT
Thank you for explaining these subtleties, the rationale makes sense and the fix you suggested seems manage all these cases properly.

Note that I think we should still use ${POSIXLY_CORRECT+x} for the reasons pointed out in  FS#46450 .
Also the use of -a is inherently ambiguous and less readable IMHO.
Comment by Sébastien Luttringer (seblu) - Saturday, 02 May 2020, 15:17 GMT
Well, ${POSIXLY_CORRECT+x} without quoting doesn't works with dash. That's why I changed.

$ bash --posix -x -c 'test "$BASH" -a "$PS1" -a -z ${POSIXLY_CORRECT+x} -a "${0#-}" != sh -a -r /etc/bash.bashrc'
+ test /usr/bin/bash -a '' -a -z x -a bash '!=' sh -a -r /etc/bash.bashrc
$ zsh --emulate sh -x -c 'test "$BASH" -a "$PS1" -a -z ${POSIXLY_CORRECT+x} -a "${0#-}" != sh -a -r /etc/bash.bashrc'
+ test '' -a '' -a -z -a zsh '!=' sh -a -r /etc/bash.bashr
$ dash -x -c 'test "$BASH" -a "$PS1" -a -z ${POSIXLY_CORRECT+x} -a "${0#-}" != sh -a -r /etc/bash.bashrc'
+ test -a $ -a -z -a dash != sh -a -r /etc/bash.bashrc
dash: 1: test: -a: unexpected operator
Comment by Stéphane Gleizes (psydev) - Saturday, 02 May 2020, 15:35 GMT
Ok, couldn't we just quote it then? IIUC it would not break the behavior.
Comment by Eli Schwartz (eschwartz) - Sunday, 03 May 2020, 07:07 GMT
test ... -a ...

is a deprecated, ill-advised construct. POSIX defines the "-a" and "-o" binary primaries as "many implementations will continue to support these obsolescent forms", while noting that they are error prone.

As obsolescent features, a future version of POSIX may remove them entirely, and strictly conforming POSIX applications MUST NOT use them.

As Stéphane pointed out, you don't even get enhanced readability as a consolation prize for using them. Quite the reverse.

There is zero downside to using multiple invocations of the test utility chained together with shell syntax '&&'. Please switch back, and for safety, forget that these obsolescent features exist.
Comment by Sébastien Luttringer (seblu) - Sunday, 03 May 2020, 14:17 GMT
Thanks for pointing the POSIX standard extra information about this syntax. It completely went out of my mind.

For rationale of the initial switch, the "-a" form is shorter, without the useless repetition of the test word, which IMHO improve readability. But, Indeed, the problem of parsing makes them undesirable.

Loading...