sh

Invoke the Bourne shell interpreter

Syntax

sh [options] [file [arguments]]

Basic Usage

Start interactive shell

sh

Starts an interactive Bourne shell session.

Execute shell script

sh script.sh

Executes the specified shell script.

Execute script with arguments

sh script.sh arg1 arg2

Execute commands from stdin

echo "echo 'Hello World'" | sh

Common Options

  • -a: Export all variables
  • -c string: Execute commands from string
  • -e: Exit on any error
  • -f: Disable filename generation
  • -h: Remember command locations
  • -i: Interactive mode
  • -k: All assignment arguments are placed in environment
  • -l: Make shell a login shell
  • -m: Enable job control
  • -n: Read commands but don't execute
  • -o option: Set option by name
  • -p: Turn on privileged mode
  • -r: Restricted shell
  • -s: Read commands from standard input
  • -t: Exit after reading and executing one command
  • -u: Exit on undefined variable references
  • -v: Print shell input lines
  • -x: Print commands and their arguments

Practical Examples

Start interactive shell

sh -i

Execute script with debugging

sh -x script.sh

Execute script with error exit

sh -e script.sh

Execute single command

sh -c "echo 'Current directory:' && pwd"

Execute script in restricted mode

sh -r script.sh

Execute script with verbose output

sh -v script.sh

Test script syntax without execution

sh -n script.sh

Execute script as login shell

sh -l script.sh

Shell Options

Common Shell Options

  • errexit: Exit on any command failure
  • nounset: Exit on undefined variable references
  • xtrace: Print commands before execution
  • verbose: Print shell input lines
  • noglob: Disable filename expansion
  • monitor: Enable job control
  • hashall: Remember command locations
  • allexport: Export all variables

Bourne Shell Features

Key Features

  • Command Execution: Execute commands and programs
  • Variable Handling: Set and use shell variables
  • Control Structures: if, while, for, case statements
  • File Redirection: Input/output redirection and pipes
  • Job Control: Background and foreground process management
  • Function Definition: Define and call shell functions
  • Arithmetic: Basic arithmetic operations

Best Practices

When to Use

  • Executing shell scripts
  • Starting interactive shell sessions
  • Running commands in Bourne shell context
  • Testing shell script compatibility
  • Debugging shell scripts

Important Notes

  • Bourne shell is the original Unix shell
  • Many modern systems use bash as the default shell
  • Bourne shell has limited features compared to bash
  • Use -x option for debugging shell scripts
  • Use -e option for error handling in scripts
  • Bourne shell is POSIX compliant
  • Consider using bash for more advanced features

See Also

  • bash - Bourne Again Shell
  • ksh - Korn Shell
  • zsh - Z Shell
  • set - Set shell options