4

I made some researches about the files with bash and sh extensions. Most of the people and resources say that if a file has bash extension, then it contains bash scripts. Likewise, the file with sh extensions contains sh scripts.

However, I cannot find the differences between bash and sh scripting. There are some courses and articles which aim to teach the people to write scripts on shell, and all of them has the title shell scripting.

In this point, which one does shell scripting correspond to ?

Bash Scripting or Sh scripting.

What I try to understand is what is the difference between bash and sh scripting.

1
  • Some tooling doesn't recognize files as scripts without .sh file extension. Commented Sep 28, 2024 at 14:08

2 Answers 2

19

File names in POSIXland don't have "extensions". A . in a filename is no different from any other character and has no specific meaning other than those that might be attributed to them by meatbags such as ourselves.

One could hope that any file with a name ending in .bash would be a script meant to be executed via the bash shell, but there is no guarantee of this.

Indeed, it's quite common to give all shell scripts a suffix of .sh no matter which interpreter is intended for their use, as the shebang line should properly specify which shell should be used to execute such a file.

sh and bash are two different, but related, shells; two amongst many others such as ksh, csh, zsh, fish, ash, dash, and yet more others.

Each shell has its own syntax, capabilities, mannerisms, and foibles; some shells are largely compatible with each other (generally any script written for sh can also be run in bash or many other shells), but some are not.

1
  • Isn't sh simply the machine's default shell (whether bash or ash or whatever)? It's very likely to be a POSIX-compliant shell, and may use different settings when run as sh, but it's not necessarily a separate implementation or standard. Commented Jan 21, 2019 at 14:56
2

The only significant issue I find day-to-day between bash and sh is the comparison operator. In bash the double-equals operator == can be used for string comparison:

if [ "$var" == "foo" ]; then  # bash

But in sh this is a syntax error, as it expects single-equals.

if [ "$var" = "foo" ]; then   # sh

I am not saying this is the only difference, but this is a commonly found issue.

1
  • 1
    Search for bashisms to find out more differences. Commented Jan 18, 2019 at 12:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.