A Deep Dive into the Unique Aspects of 'sh' and 'bash'

A Deep Dive into the Unique Aspects of 'sh' and 'bash'

Shell scripting is a cornerstone of managing Unix and Linux systems. The Bourne shell ('sh') and the Bourne Again shell ('bash') have distinct capabilities that can significantly impact scripting strategies. This article explains 14 key features, their functionalities, and how each shell handles them.

Why it matters

When containerizing microservices, understanding the intricacies of different shell environments like the Bourne shell ('sh') and the Bourne Again shell ('bash') is more than a mere technicality – it's a necessity as application developers tend to want to have the smallest containers possible.

Common Differences

1. Process Substitution

  • Description: Allows a command’s output to be used as a file input to another command.
  • Bourne shell ('sh'): No support.
  • Bourne Again shell ('bash'): Example: cat <(ls)

2. Arrays

  • Description: A data structure that stores a collection of elements.
  • Bourne shell ('sh'): Does not support arrays.
  • Bourne Again shell ('bash'): Example: arr=(one two three); echo ${arr[1]}

3. Brace Expansion

  • Description: Generates arbitrary strings or sequences.
  • Bourne shell ('sh'): Absent.
  • Bourne Again shell ('bash'): Example: echo {a..c}

4. String Comparison

  • Description: Compares two strings for equality.
  • Bourne shell ('sh'): Limited to =.
  • Bourne Again shell ('bash'): Example: [[ "$a" == "$b" ]]

5. Local Variables

  • Description: Variables that are local to the function they are declared in.
  • Bourne shell ('sh'): No local keyword.
  • Bourne Again shell ('bash'): Example: local local_var=value

6. Arithmetic Operations

  • Description: Performs basic arithmetic calculations.
  • Bourne shell ('sh'): Uses expr or $(( )).
  • Bourne Again shell ('bash'): Example: result=$((3 + 2)) or (( result = 3 + 2 ))

7. C-style for Loop

  • Description: A loop syntax similar to C language.
  • Bourne shell ('sh'): Not available.
  • Bourne Again shell ('bash'): Example: for ((i = 0; i < 3; i++)); do echo $i; done

8. Here Strings

  • Description: Redirects a string into an input.
  • Bourne shell ('sh'): No support.
  • Bourne Again shell ('bash'): Example: cat <<< "Hello"

9. Double Bracket [[ ... ]]

  • Description: A test builtin command.
  • Bourne shell ('sh'): Not supported.
  • Bourne Again shell ('bash'): Example: if [[ -f "$file" ]]; then echo "File exists"; fi

10. Regex Matching

  • Description: Matches strings using regular expressions.
  • Bourne shell ('sh'): No regex operator.
  • Bourne Again shell ('bash'): Example: [[ $text =~ ^[a-zA-Z]+$ ]]

Bonus Differences

11. Bash Debugging Environment

  • Description: Tools and options for debugging scripts.
  • Bourne shell ('sh'): Basic debugging options.
  • Bourne Again shell ('bash'): Example: set -x for expanded debugging.

12. History Expansion

  • Description: Reuses commands from the shell’s history.
  • Bourne shell ('sh'): Limited history features.
  • Bourne Again shell ('bash'): Example: !! repeats the last command. or !25 will repeat the 25th command in the user's history

13. Tilde Expansion

  • Description: Expands ~ to specific directories, usually home directories.
  • Bourne shell ('sh'): Limited tilde expansion.
  • Bourne Again shell ('bash'):Example: cd ~ to navigate to the home directory.

14. Advanced Variable Features

  • Variable SubstitutionDescription: Replaces or modifies parts of a variable's value.Bourne Again shell ('bash'): Example: echo ${var/old/new} to replace 'old' with 'new' in $var for the command. This does not change the value of $var unless $var is redefined.
  • Variable DefaultsDescription: Uses a default value if a variable is unset or null.Bourne Again shell ('bash'): Example: echo ${var:-default} to use 'default' if $var is unset.

Conclusion: Tailoring Scripts for the Shell Environment

Each feature discussed plays a pivotal role in shell scripting, and understanding how 'sh' and 'bash' handle them can significantly influence script functionality and portability. Whether it's leveraging 'bash's advanced features for complex scripts or ensuring compatibility with the more universally available 'sh', awareness of these differences is key for effective script writing.

要查看或添加评论,请登录

Robert J.的更多文章

社区洞察