LinuxCommandLibrary

sh

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 [-cefinvx] [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

-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
-n
Read commands but do not execute (syntax check)
-v
Print shell input lines as they are read
-x
Print commands and arguments as they are executed
-u
Treat unset variables as an error
+option
Turn off option

POSIX FEATURES

Variables: NAME=value, $NAME, ${NAME}
Quoting: 'literal', "interpolated", \escape
Conditionals: if-then-elif-else-fi, case-esac
Loops: while-do-done, for-in-do-done, until-do-done
Functions: name() { commands; }
Tests: [ condition ], test condition
Arithmetic: $((expression))

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)

> TERMINAL_GEAR

Curated for the Linux community

Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

Lenovo Gen 13 ThinkPad X1 Carbon Touchscreen Laptop with Intel Ultra 7 265U Processor, 14" WUXGA 100% sRGB, 32GB DDR5 RAM, 1TB NVMe Performance SSD, FHD+ IR+ RGB Camera, and Windows 11 Pro

$1,589.00
New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

New Raspberry Pi 3 Model B+ Board Raspberry PI 3B+

$54.00
Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

$169.99
Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

Bmax Mini PC B2 Power Intel 5205U 8GB DDR4 256GB M.2 NVMe SSD ,HDMI 1.4&DP 1.2, USB3.2,4K Dual Display 2.4G/5G WiFi & BT5.0 Mini Desktop Computers /Office/Design

$169.99
nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

nuphy Air75 V2 Portable 75% Mechanical Keyboard,Wireless Keyboard

$129.95
BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

BOSGAME Mini PC N95, Intel 12th Mini Computers| 16GB DDR4 RAM| 512GB M.2 2280 SSD| 4K 60HZ Triple Displays by Dual HDMI+Type-C, 6W TDP Quiet Mini PC, WIFI5, BT 5.0 Office

$239.99