4

So, there are many types of shells in Linux...

Different Types of Shells in Linux

The Bourne Shell (sh) ...
The GNU Bourne-Again Shell (bash) ...
The C Shell (csh) ...
The Korn Shell (ksh) ...
The Z Shell (zsh)

I have a project where I have to put together a script for the Bourne shell, starting with shebang #!/bin/sh.

But whenever I use Google to learn about some detail, I always get tons of results for bash, which I suspect are ok to use with sh, but not always...

So I want to tell Google, "No! Give just stuff for the original Bourne shell!". But people don't type the string "Bourne shell" into most of their web pages, so Google can't help me directly with that.

Adding "sh" to my search query doesn't help much, it matches .sh file extensions, and it matches bash!

Adding "#!/bin/sh" to my search query is somewhat helpful - but I will see only results in scripts, when sometimes the simple anwsers are presented as terminal commands (which we can then adapt for use in scripts).

I know there won't be any "silver bullet" perfect solution, but in a spirit of "tips and tricks", would any of you suggest clever ways to narrow down my results like I intend?

Or is the only solution to use a handful of authoritative Bourne shell documentation sources? If so, which ones?

pgr
  • 151
  • 3
  • 3
    Is this a question about how to use Google Search? – Kusalananda May 22 '21 at 10:14
  • They're probably asking how to search/get result for a specific shells, and maybe confuse that some shells's code have compatibility between each other or/and can be adapted to another. @Kusalananda – Nordine Lotfi May 22 '21 at 10:26
  • 1
    @NordineLotfi Well, the manual for the sh shell on any given system is readily available with man sh. – Kusalananda May 22 '21 at 10:41
  • Indeed :) That's what your and my answer explained too. (I already know you know this but was more of an answer to OP) @Kusalananda – Nordine Lotfi May 22 '21 at 10:42
  • yes, this is a question about using google search. Searching for common words or sequences of letters (like "sh") is always going to be difficult, there's always going to be lots of false positive matches. Worse, it's often impossible or impractical to exclude stuff - e.g. you could add -bash to the search, but many results with examples specific to sh will also mention bash (and you won't want them excluded). Such is life. – cas May 22 '21 at 10:45
  • 3
    I’m voting to close this question because this is about using search engines, not Unix. – Barmar May 22 '21 at 22:11
  • @barmar it isn't about search engines. I know about search engines and am not asking for knowledge about them. It is about Unix: which terms go together with which. For example, before reading the answers, I didn't know or understand what POSIX was, or that sh is not the classic Bourne shell, but something newer. This question boils down to "does sh go by other names? Does it frequently appear with which other terms?". I am quite happy with the replies I got, BTW. – pgr May 23 '21 at 10:16
  • Your question is about how to find the information in search engines. Search engines don't have artificial intelligence, they don't understand English. So even if you use the term that any knowledgeable computer programmer would understand to mean Bourne shell, you can't expect searches to return the information you want. – Barmar May 24 '21 at 02:38
  • So your question is essentially "How can I convince Google to limit its search results to Bourne shell?" And I think the answer is probably that you can't. – Barmar May 24 '21 at 02:40
  • As I wrote, there is no "silver bullet", no perfect answer, but that doesn't mean there is no answer at all. The answers given prove that: there's "POSIX" which I didn't know about. And the canonical documentation sites listed in the answers. This is a perfectly normal question with good answers, no point in closing it. You and some people voted it down, other people voted it up, the answers are also voted up. It's a display of some level of participation, approval and appreciation for this Q&A. Not what you'd get if the question was really off-topic. But ok... peace. – pgr May 24 '21 at 11:21

4 Answers4

10

You most likely aren't actually interested in the historical Bourne shell (which is not commonly available on most current UNIX systems), but in shells that implement the current POSIX specification for the sh shell.

See also:

  • The currently accepted answer to the U&L question What does it mean to be "sh compatible"? goes into great detail describing the history and "pedigree" of Unix shells.

  • The command man sh on the system you are working with will show you the manual for the sh shell on that system. This will be a shell implementing the POSIX syntax and grammar, possibly by changing the way the shell would normally behave if invoked using its own name (like bash and zsh does when invoked as sh, for example).

Kusalananda
  • 333,661
5

In my experience, if you're looking for help about how to do a given thing using your #!/bin/sh shell you could prepend your Google query with one of the following:

  • POSIX - it will lead you to discussion about implementing a given feature in POSIX-compatible shells or in POSIX-compatible manner in shells that implement more functions that are not required by POSIX such as Bash. (usually you'll end up at https://unix.stackexchange.com)

  • dash - dash is /bin/sh Ubuntu which is the most popular desktop Linux distro. It's used by cron and other tool that run commands using /bin/sh so many questions have been asked and answered

  • busybox - busybox comes with its own shell which is not as huge as Bash or ZSH so if something works in busybox shell it should also work with your local minimalist /bin/sh, whatever it is.

3

Here something that I would say is the best way to tackle this:

commonsense

Now while this may sounds obvious or rude, you may ask "what commonsense?":

  • bash is sometimes symlinked to sh on certain distros
  • sh is sometimes symlink to ash (busybox)
  • bash script usually work fine (except for certain syntaxes) under sh, and vice versa (same for ash-sh)
  • Try them. (literally, as it probably wouldn't be obvious that whatever snippet have errors or not, unless you use shellcheck or have more experience)
  • Try different keywords combination (this is obvious but harder to explain in details without making this post longer)
  • Try different search engine (most have similar result, but not always, if ever exact same result, with the same keywords)

You can apply/expand the above for other shells too. Just don't hesitate to try whatever snippet/code you find (I find -x or set -x for debugging and shellcheck to be useful help when doing that).

Of course, it is still recommended to use the related documentation for each shells when you're in doubt or stumble on problems.

Additional resources:

Trying is part of learning. Good luck!

3

Of the common Linux distributions, I think most (e.g. CentOS and Arch) use Bash as /bin/sh. The main exception is with Debian and Ubuntu (and derivatives), which use Dash. Ubuntu also has a helpful wiki page identifying the most common "Bashisms": https://wiki.ubuntu.com/DashAsBinSh

Note that the original Bourne shell is slightly different from the POSIX shell that e.g. Dash implements. You're not going to find that on Linux systems without explicitly looking for it, e.g. the Heirloom Shell.

ilkkachu
  • 138,973
  • The heirloom is a buggy port of the original Bourne Shell that introduces deviations you are not interested in if you are interested in the historic behavior. If you like a clean port of the original Bourne Shell, have a look at obosh from the schilytools. – schily May 23 '21 at 20:56