LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

sh

POSIX-compliant command interpreter

TLDR

Start an interactive shell
$ sh
copy
Execute a script
$ sh [script.sh]
copy
Execute commands from a string
$ sh -c "[command1; command2]"
copy
Execute script with verbose output (show commands)
$ sh -x [script.sh]
copy
Check syntax without executing
$ sh -n [script.sh]
copy
Read commands from stdin
$ echo "echo hello" | sh
copy

SYNOPSIS

sh [-aCefimnsuvx] [script [argument...]]

DESCRIPTION

sh is the POSIX-compliant command interpreter (shell). It provides the standard shell scripting environment and is guaranteed to be available on all Unix-like systems.On many Linux systems, /bin/sh is a symbolic link to another shell (dash, bash, etc.) running in POSIX-compatibility mode. This provides both compatibility and performance benefits.Shell scripts beginning with #!/bin/sh use POSIX sh, ensuring maximum portability. Scripts needing bash-specific features should use #!/bin/bash instead.The shell reads commands from standard input, a file, or the -c argument. It supports variables, control flow (if, while, for, case), functions, pipelines, and I/O redirection.

PARAMETERS

-a

Export all variables that are modified or created to the environment
-C
Prevent output redirection from overwriting existing files (noclobber)
-c string
Execute commands from string
-e
Exit immediately if a command exits with non-zero status
-f
Disable filename globbing (wildcard expansion)
-i
Interactive shell
-m
Enable job control (default for interactive shells)
-n
Read commands but do not execute (syntax check)
-s
Read commands from standard input
-u
Treat unset variables as an error
-v
Print shell input lines as they are read
-x
Print commands and arguments as they are executed
+option
Turn off option

CONFIGURATION

~/.profile

Per-user login shell initialization file, executed for login shells.
/etc/profile
System-wide login shell initialization file, executed before ~/.profile.
ENV
Environment variable pointing to a file executed at interactive shell startup.

POSIX FEATURES

Variables: NAME=value, $NAME, ${NAME}Quoting: 'literal', "interpolated", \escapeConditionals: if-then-elif-else-fi, case-esacLoops: while-do-done, for-in-do-done, until-do-doneFunctions: name() { commands; }Tests: [ condition ], test conditionArithmetic: $((expression))

INSTALL

sudo apt install dash
copy
sudo dnf install dash
copy
sudo pacman -S dash
copy
sudo apk add busybox-binsh
copy
sudo zypper install dash
copy
brew install dash
copy
nix profile install nixpkgs#dash
copy

CAVEATS

POSIX sh lacks many bash features: arrays, [[ ]], brace expansion, process substitution, many string manipulations. Write portable scripts or explicitly require bash.The -e option can cause unexpected exits. Commands in conditions (if, while) or with || / && don't trigger exit on failure.Different systems link /bin/sh to different implementations (dash, bash, ksh). Test scripts on target systems or use explicit interpreters.

SEE ALSO

bash(1), dash(1), zsh(1), ksh(1), csh(1)

Braincup
Open source brain training for math, memory and focus
Braincup mini-games
41 mini-games · Apache-2.0
No ads · No tracking
Play in browser
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid
276 stars
From the maker of Linux Command Library
Copied to clipboard
Braincup
Open source brain training for math, memory and focus. 41 mini-games, from mental arithmetic to Sudoku, N-Back and Solo Chess.
Apache-2.0 licensed · No ads · No tracking · No account
From the maker of Linux Command Library
Download Braincup on the App StoreGet Braincup on Google PlayGet Braincup on F-Droid