The Linux sh command: standard POSIX shell

Introduction

In the Linux world, the command line is a key tool for managers, developers and advanced users. Although many associate the terminal with bash, there is a more minimal and standardized alternative known assh. This command invokes the standard POSIX shell, whose behavior is defined by the POSIX.1-2008 specification and guarantees portability between different Unix-like systems. In this article we will explore what isshits main characteristics, how to use it effectively and in what situations it is preferable to other shells.

What's sh?

The commandshIt is not a specific shell, but an interface that points to the implementation of POSIX shell of the system. In most Linux distributions,shis a symbolic link todash(Debian Almquist Shell) or abashwhen running in POSIX mode. Regardless of the underlying implementation, when invokingshensures that the interpreter strictly follows the POSIX standard, avoiding own extensions of bash or other shells.

Main characteristics

  • POSIX compatibility: only allows syntax and utilities defined in the standard.
  • Reduced resource consumption: ideal for start scripts and embedded environments.
  • Previsibility: the behavior is the same in any system that meets POSIX.
  • Absence of advanced features: does not include associative arrangements, complex parameter expansion or interactive cleansing.

Basic use

To run a simple order withshsimply write it in the terminal:

sh -c "echo Hola mundo"

The option-ctoshread the order from the next chain. Another common way is to create a script file and give you the execution permission:

#!/bin/sh
printf 'Versión del kernel: %s\\n' "$(uname -r)"

Save the content asversión.sh, runchmod +x versión.shand then./versión.sh.

Practical examples

  • Check if a directory exists and create it if not:
  • #!/bin/sh
    [ -d "$HOME/backup" ] || mkdir -p "$HOME/backup"
    
  • Browse files from a directory and show their size:
  • #!/bin/sh
    for f in *; do
      [ -f "$f" ] && printf '%s: %s\\n' \"$f\" \"$(stat -c%s \"$f\")\"
    done
    
  • Run a chain of commands with pipes:
  • #!/bin/sh
    cat /etc/passwd | grep '^/home' | cut -d: -f1
    

Bash differences

  • Bash includes features like arrangements,[[ ]]for advanced and$()with unlimited nesting.
  • In POSIX mode (bash --posix) bash is approachingshbut it still allows certain extensions if not strictly activated.
  • The scripts they useshare more portable; those that depend on bash can fail in minimalist systems such as Alpine Linux or very low container environments.

When to use sh

It is recommended to useshin the following scenarios:

  • System initialization scripts (/etc/init.d, systemdservice files that callExecStart).
  • Container frames where a minimum image is sought (e.g.,alpine).
  • When you need to ensure that the script runs equally in any Unix-like distribution.
  • For simple management tasks where advanced bash functions are not required.

Conclusion

The commandshrepresents the essence of the standard POSIX shell: simplicity, portability and predictability. Although it lacks many of the comforts it offers bash, its value is to provide a reliable environment for scripts that must be run in various systems without surprises. Know when and how to useshis an essential skill for any professional who works with Linux and wants to write robust and portable code.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish