Question

sh

Thought

Interpreting the "sh" request

The user typed “sh,” which could mean they want help with a shell script or it might’ve been a typo. I’ll provide an explanation of what “sh” is, how to use it, and the difference with bash. I’ll include basic instructions for running commands and wrap it up with a question at the end to clarify if needed.

Thought for 13s

Answer

Did you mean sh as in the Unix shell (POSIX “Bourne shell” interface)?

If so, here’s the practical cheat sheet.

What sh is

  • sh runs a POSIX-compatible shell (the “standard” shell language).
  • On many systems, /bin/sh is not Bash:
    • Debian/Ubuntu often link /bin/shdash (faster, more strictly POSIX).
    • macOS commonly runs an older Bash in “sh mode,” but details vary by version.

Run a script with sh

sh
sh script.sh

Make a script executable (recommended)

  1. Put a shebang on the first line:
sh
#!/bin/sh echo "Hello"
  1. Make it executable and run it:
sh
chmod +x script.sh ./script.sh

Common “safe mode” options

sh
set -eu # -e: exit on error # -u: error on unset variables

(“pipefail” isn’t POSIX, so it may not work in sh.)

POSIX vs Bash gotchas (quick)

If you use sh, avoid Bash-only features like:

  • [[ ... ]] (use [ ... ])
  • arrays arr=(a b)
  • source file (use . file)
  • brace expansion {1..5}

Example POSIX test:

sh
if [ -f "$1" ]; then echo "file exists" fi

If you tell me what you meant by “sh” (run a .sh file? write a shell script? something else?), I’ll tailor it.

Share this Q&A