-4

bash is the default login shell in Ubuntu. Most users use bash for scripting. Some answers use sh as the interpreter, even if the question mentioned bash (force of habit, I assume). (For example, a question about adding commands to startup, or creating launchers.) This can lead to problems where the user has unknowingly used a bashism, and then later on:

But that didn't work! I get some weird error!!1! :(

This also applies to answers where complex shell commands have to be used, such as:

sudo sh -c ...
gnome-terminal -x sh -c ...

Where using bash may simply make things more easier (at the cost of two letters).

Those who are smart enough to use a different shell known enough to substitute the correct shell.

Unless sh-compatibility is known for certain, we should refrain from using it at all.

1 Answer 1

7

I see no reason not to use sh. On the other hand, answers that use it and include bashisms should be edited. On Ubuntu, sh is actually dash not sh but, again, that should be irrelevant in most cases.

So, a simple answer suggesting something like

#!/bin/sh
echo foo

is fine. There are valid reasons to use sh (whether it be actual sh or dash or even bash called as sh which changes its behavior). Startup scripts for example benefit from the greater speed of a lighter shell. An answer that uses the more advanced features of bash (such as arrays) will not work with sh so it is wrong and should be downvoted/edited but I see no reason to ban sh outright.

So no, I would suggest the inverse. Unless something is known not to work with sh, use it freely. If nothing else, it will make answers more portable which is a good thing despite the site's being focused on Ubuntu.

11
  • 1
    IMHO the arrival of Systemd is as big an indication as anything that portability is not that big a concern any longer in the Linux world. Sometimes we should shrug off old cruft. Keep things portable on Unix & Linux, sure. But if we can't make allowances for what Ubuntu is, then what's the difference between AU and U&L? If a non-Ubuntu user uses an answer from here they should think about porting commands to their setup. tl;dr: Screw portability. Bash is portable enough across Linux systems and startup scripts are special cases.
    – muru
    Commented Sep 24, 2014 at 12:05
  • 3
    @muru agreed, portability is not very important here. On the other hand, why not? sh is simply better than bash in most cases. Unless you need the advanced features of bash, using it only makes your scripts slower. Anyway, my point is that a blanket ban of sh makes no sense. How would it be enforced anyway?
    – terdon
    Commented Sep 24, 2014 at 12:09
  • I know we cannot ban it. The post is intended as a definitive post I can link to when advising others to use bash. The thing is we slip into non-portable commands without even noticing them in many things, and sticking to sh without any benefit (if someone really need faster ifs and whiles, I'd tell them to use C) is a pointless exercise. Especially in the case of the simple scripts that you say run fine on sh, what speed benefit is there?
    – muru
    Commented Sep 24, 2014 at 12:56
  • 1
    @muru AHA so I can revert the changes her askubuntu.com/a/527787/72216 (just kidding) Commented Sep 24, 2014 at 13:27
  • @JacobVlijm If you do, then I strongly suggest that you edit the question to remove references to bash. Giving the impression that this will work for all bash scripts is what I have a problem with (I don't have a problem with sh).
    – muru
    Commented Sep 24, 2014 at 13:32
  • @muru nono, I'll leave it like this, I don't think it's a big issue on a .desktop file, and the question is indeed on bash, as the title sais. Commented Sep 24, 2014 at 13:41
  • @muru heh, you'd be surprised! In some cases bash can be 4 times slower than dash. Everything depends on the use case. Anyway, all I'm saying is that the default system shell for Ubuntu (dash disguised as sh) should be fine. Just fix bashisms if and when they appear.
    – terdon
    Commented Sep 24, 2014 at 14:09
  • I'm not convinced. The default system shell being dash is not enough as an argument for sh/dash as long as the default login shell is bash.
    – muru
    Commented Sep 24, 2014 at 14:12
  • @muru there are Debian/Mint and even Arch users that can find a answer that helps them here, specially if it involves any shell, so I prefer portability even when we are a Ubuntu-only site.
    – Braiam
    Commented Sep 24, 2014 at 19:36
  • Oh, btw I had to fix a tcsh script that was filled of bashism (actually Bourneshell-ism), and was more or less fun.
    – Braiam
    Commented Sep 24, 2014 at 19:57
  • @Braiam and I have often found solutions on Arch Wiki, but not due to any great love of portability that they have (they started the great python3 v python2 for python thing, after all), but because they have clear, concise documentation with great examples. (Debian/Mint also use bash as the default login shell, btw and I suppose Arch lets you do what you want to do.)
    – muru
    Commented Sep 24, 2014 at 23:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .