LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Is it safe and sensible to link sh to dash? (https://www.linuxquestions.org/questions/slackware-14/is-it-safe-and-sensible-to-link-sh-to-dash-4175694226/)

M0M0 04-24-2021 05:10 PM

Is it safe and sensible to link sh to dash?
 
In theory, dash should be faster than bash. So I was wondering if you should link sh to dash (instead of bash). Can this cause problems with existing scripts under Slackware?

I also noticed that /bin/sh is a link to bash, but bash is started in a certain mode: The prompt is sh-5.1 instead of bash-5.1. So is this already a script-optimized version of bash? If so, does it even make sense to link sh to dash or is this optimized version of bash just as fast?

Emerson 04-24-2021 05:17 PM

I would never mess with system shell, it is not worth the potential trouble, IMHO. User shell is another story, whatever suits you best.

M0M0 04-24-2021 05:23 PM

My user shell is zsh, I think using dash as a user shell is insane. Its main purpose is to not have to worry about all the features that a convenient user shell should have. It's optimized for running scripts and being a system shell (it's the default system shell on debian). But what you said is true, there could be trouble changing the system shell. That's the reason I asked this question.

slac 04-24-2021 05:30 PM

Whether you have problems or not changing /bin/sh to dash will highly depend on the quality of the scripts of the system.

You can check at current ChangeLog.txt and see:

Code:

Sat Apr 24 18:55:05 UTC 2021
a/pkgtools-15.0-noarch-40.txz:  Rebuilt.
  Change several script shebangs to #!/bin/bash. Thanks to mumahendras3.

... And also:

Code:

Fri Apr 23 19:13:09 UTC 2021
a/sysvinit-scripts-15.0-noarch-2.txz:  Rebuilt.
  Use #!/bin/bash for these scripts so that bashisms don't cause script issues
  if /bin/sh is some other shell. Thanks to mumahendras3.
  rc.S: Use GazL's proposals for detecting/mounting /proc and /sys.

As you can see in those logs, some scripts of the system have been changed from #!/bin/sh to #!/bin/bash. Why? Because such scripts were using code that is not POSIX compliant, so it makes sense to use bash since bash-isms are being used there.

If there is a script that is using non POSIX compliant functionality and if the script itself is having #!/bin/sh in the first line and /bin/sh is linked to an only POSIX compliant shell then the script may not work at times or just entirely not work.

As I said earlier, whether you have problems or not changing /bin/sh to dash will depend in that the scripts that are not POSIX compliant are using a proper shebang link to the shell that they have been created for.

kaott 04-24-2021 05:32 PM

I do believe the reason the prompt shows up as sh-5.1 instead of bash-5.1 is because bash checks to see how it was started ($0) and it sees /bin/sh, so it adjusts the prompt accordingly. The man page says
Quote:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.

I changed the /bin/sh link to dash the other day after mumahendras3 had got some fixes in the rc scripts, nothing has catastrophically broke so far. The output of the rc.cups script was a bit malformed, but I just changed the shebang in that to bash as well. I think with the recent changes to pkgtools shebang that might fix any "serious" issues that might arise, but this is pure speculation.

Prior to this I was looking into POSIXify some of the rc scripts and pkgtools scripts, but I didn't put too much effort into it as I wasn't sure if the changes would even be accepted. Some of the rc scripts would take a lot of effort to do so, probably with very little or no gain in performance, but would have a lot more complexity (eg rc.inet1 makes heavy use of arrays and recreating with simulated arrays would be complex and not worth it as a whole.)


Some tools you can use if you are really curious is checkbashisms and shellcheck. These can point out if you have a script that claims to be POSIX shell script but it uses some bashisms.

Do note that dash has some additional extensions (eg echo accepts some flags like -n) beyond "strict" POSIX compliance.

But changing the /bin/sh link to dash should be done with caution.

Didier Spaier 04-24-2021 05:53 PM

To add some fun the POSIX specification of the Shell Command Language, in its chapter 2.1 Shell Introduction states:
Quote:

If the first line of a file of shell commands starts with the characters "#!", the results are unspecified.

jmccue 04-24-2021 06:48 PM

No, someone tried something like that many years ago, but I think it was ash. That caused a lot of startup issues.

jmgibson1981 04-24-2021 07:58 PM

I'm a firm believer in experimentation. That being said my view is also that this type of thing is why extremely user friendly virtualization apps exist. I'd use Virtual Box for this one as you can easily snapshot the image before you do it.

Pithium 04-24-2021 08:20 PM

FWIW I recently found myself in the unfortunate position of trying to write a simple shell script on an Ubuntu system. It's a simple script using SH/BASH compatible syntax but it failed hard when I set the starting line to #!/bin/sh.

On Ubuntu, sh is a link to dash and there are some syntax differences. I changed the line to #!/bin/bash and never looked back. If dash is faster it's probably because it skimps on some features and ultimately breaks compatibility with BASH scripts. Not worth it in my opinion.

ZhaoLin1457 04-24-2021 08:20 PM

Quote:

Originally Posted by M0M0 (Post 6244594)
In theory, dash should be faster than bash.

And even faster is systemd. It's sensible faster. We should move to systemd, according with you?

wpeckham 04-24-2021 08:30 PM

Quote:

Originally Posted by ZhaoLin1457 (Post 6244640)
And even faster is systemd. It's sensible faster. We should move to systemd, according with you?

Surely you are not suggesting that systemd is a shell or shell replacement? The two factors are very different.

ZhaoLin1457 04-24-2021 08:33 PM

Quote:

Originally Posted by wpeckham (Post 6244645)
Surely you are not suggesting that systemd is a shell or shell replacement? The two factors are very different.

I see no matter where a faster shell would matter, other than the init stage.

For everything else, it is just a celestial debate on a Shao-Lin temple. Not meaningful for us, the mortal sinners.

Emerson 04-24-2021 08:58 PM

I have no systemd, but I hear it does not boot faster any more, became too bloated. The advantage over OpenRC with parallel boot enabled was marginal anyway. Faster boot is just one of those salesman tales.

M0M0 04-25-2021 01:22 AM

Quote:

Originally Posted by ZhaoLin1457 (Post 6244647)
I see no matter where a faster shell would matter, other than the init stage.

Well, It matters all the time you run a POSIX compliant shell script..

M0M0 04-25-2021 01:26 AM

Quote:

Originally Posted by Pithium (Post 6244639)
FWIW I recently found myself in the unfortunate position of trying to write a simple shell script on an Ubuntu system. It's a simple script using SH/BASH compatible syntax but it failed hard when I set the starting line to #!/bin/sh.

No if dash can't run it it is not a POSIX compliant shell script. You wrote a bash script not a shell/sh script. You should only use #!/bin/sh if your script is POSIX compliant.


All times are GMT -5. The time now is 10:17 AM.