← 返回命令列表

Linux command

sh 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Start an interactive shell

sh

Execute a script

sh [script.sh]

Execute commands from a string

sh -c "[command1; command2]"

Execute script with verbose output

sh -x [script.sh]

Check syntax without executing

sh -n [script.sh]

Read commands from stdin

echo "echo hello" | sh

说明

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.

参数

-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

FAQ

What is the sh command used for?

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.

How do I run a basic sh example?

Run `sh` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a do in sh?

Export all variables that are modified or created to the environment