Skip to content
Learn Netverks

Lesson

Step 32/36 89% through track

bash-vs-sh-bash

Bash vs sh portability

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Bash vs sh portability: the syntax, patterns, and safety habits you need before advancing in Bash.

Teams still ship Bash vs sh portability in Bash codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Bash vs sh portability in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons.

Toward the end of the track—consolidate before interview prep and production checklist lessons.

POSIX sh is the portability baseline; Bash adds arrays, [[ ]], and more. Containers and old systems may point /bin/sh to dash.

Shebang choices

#!/bin/sh    # POSIX mode on many distros
#!/usr/bin/env bash  # Bash features OK

Debian/Ubuntu sh is often dash—Bash-only syntax breaks.

Portable test

# POSIX
if [ "$var" = "yes" ]; then echo ok; fi
# Bash
if [[ $var == yes ]]; then echo ok; fi

Quote variables in [ ] to handle empty values safely.

Detect bash

if [ -n "$BASH_VERSION" ]; then
  echo "running bash $BASH_VERSION"
fi

Feature-detect when one script must run on both sh and bash.

Important interview questions and answers

  1. Q: Why [[ ]] not in sh?
    A: It is a Bash keyword—not available in POSIX test.
  2. Q: When use /bin/sh shebang?
    A: Maximum portability for minimal install scripts and some embedded environments.

Self-check

  1. What breaks if you use [[ on dash sh?
  2. What variable indicates Bash?

Tip: If you need [[ ]], do not claim POSIX-sh portability—use Bash shebang.

Interview prep

[[ in sh?

Not POSIX—Bash/ksh feature; dash sh breaks.

$BASH_VERSION?

Indicates script runs under Bash.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • #!/bin/sh portable?
  • bashisms list?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump