Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 953868 - app-alternatives/sh: add `bash --posix` as /bin/sh option
Summary: app-alternatives/sh: add `bash --posix` as /bin/sh option
Status: RESOLVED UPSTREAM
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Lowest enhancement
Assignee: Gentoo's Team for Core System packages
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-04-14 23:06 UTC by Filip Kobierski
Modified: 2025-04-21 18:21 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Filip Kobierski 2025-04-14 23:06:39 UTC
If bash is started with the --posix flag it simplifies it's behaviour to conform to POSIX standards.

Since the sh is way simpler than bash it also is faster.
I was thinking that /bin/sh could be a simple wrapper for calling bash with this single argument. This would be a depless speed improvement!

From what I can see most of the packages work with a simple posix sh and running a dash system I recall having around 3 problems in the last year, which were fixed quickly.

I have opened this bug in "new packages" category because I think adding this could be solved by another package, maybe called `bash-posix` that could be later added to app-alternatives/sh.
Comment 1 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2025-04-15 04:53:38 UTC
I guess that would mean a compiled wrapper, since you don't want /bin/sh to run via shell.
Comment 2 Mike Gilbert gentoo-dev 2025-04-15 15:58:52 UTC
(In reply to Filip Kobierski from comment #0)
> Since the sh is way simpler than bash it also is faster.
> I was thinking that /bin/sh could be a simple wrapper for calling bash with
> this single argument. This would be a depless speed improvement!

This sounds unlikely to me. Do you have any evidence of this speed improvement?
Comment 3 Filip Kobierski 2025-04-21 18:21:53 UTC
While looking for answers to Mike's question I have made a good discovery:
It turns out that:
"When invoked as sh, Bash enters POSIX mode after reading the startup files."
(https://www.gnu.org/software/bash/manual/html_node/Bash-POSIX-Mode.html)

My tests confirm that:

TEST 1: point 15
$ cat test.sh 
0myfn() { echo foo; }; type 0myfn
$ bash ./test.sh 
0myfn is a function
0myfn () 
{ 
    echo foo
}
$ sh ./test.sh 
./test.sh: line 2: `0myfn': not a valid identifier
$ dash ./test.sh 
./test.sh: 2: Syntax error: Bad function name

TEST 2: accepting `function`:
$ cat test.sh 
function myfn { echo foo; }; type myfn
$ bash ./test.sh 
myfn is a function
myfn () 
{ 
    echo foo
}
$ sh ./test.sh 
myfn is a function
myfn () 
{ 
    echo foo
}
$ dash ./test.sh 
./test.sh: 2: Syntax error: "}" unexpected

TEST 3: point 15 (not clear but exhibits different behaviour)
$ cat test.sh 
[ -n $BASH ] && echo posix? $POSIXLY_CORRECT
myfn() { echo foo; }; type myfn
$ bash ./test.sh 
posix?
myfn is a function
myfn () 
{ 
    echo foo
}
$ sh ./test.sh 
posix? y
myfn is a function
myfn () 
{ 
    echo foo
}
$ dash ./test.sh 
posix?
myfn is a shell function


This makes this proposal useless, as it is the default bash behaviour already.