LinuxCommandLibrary

sh

Execute shell scripts

TLDR

Start an interactive shell session

$ sh
copy

Execute a command and then exit
$ sh -c "[command]"
copy

Execute a script
$ sh [path/to/script.sh]
copy

Read and execute commands from stdin
$ sh -s
copy

SYNOPSIS

sh [options] [command-file | -c command-string] [arguments]

PARAMETERS

-
    Read commands from standard input.

-c command-string
    Execute the command-string.

-i
    Interactive shell. Forces the shell to be interactive.

-s
    Read commands from standard input after the arguments are processed.

command-file
    Execute commands from the specified file.

DESCRIPTION

The sh command is a shell interpreter, typically a symbolic link to another shell like bash, dash, or ksh. It reads commands from standard input or a file and executes them.
Its behavior depends on the underlying shell it's linked to. Generally, it provides a command-line interface for interacting with the operating system, executing programs, and managing files. The sh command provides basic scripting functionalities, allowing users to automate tasks, manage system processes, and perform various operations through shell scripts.
As a shell interpreter, sh handles variable assignments, control flow statements (like if, for, and while), and command substitution. It supports pipes and redirection, allowing users to chain commands together and redirect input and output. Due to historical reasons, the capabilities and syntax may vary based on which shell implementation the sh is pointing to on the system.

CAVEATS

The exact behavior of sh depends on which shell it is linked to. Scripts written for sh should generally adhere to the POSIX standard for maximum compatibility.

POSIX COMPLIANCE

To ensure portability, it's recommended to write scripts that adhere to the POSIX standard for sh. This involves using only features and syntax specified in the standard, avoiding shell-specific extensions.

INVOCATION

The shell can be invoked with or without arguments. If invoked without arguments, it reads commands from standard input. If invoked with a command-file argument, it reads commands from the specified file. If invoked with -c option, the shell execute given command.

HISTORY

sh is one of the oldest Unix shells, initially developed by Stephen Bourne at Bell Labs in the 1970s. It was widely used as the default shell on Unix systems. Over time, more advanced shells like bash, ksh, and zsh emerged, offering extended features and functionalities. While sh is still prevalent, it often serves as a symbolic link to another shell on modern systems. This allows for backward compatibility and ensures that scripts written for sh can still be executed.

SEE ALSO

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

Copied to clipboard