← 返回命令列表

Linux command

set 命令

网络

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

常用示例

Enable strict mode

set -euo pipefail

Exit immediately

set -e

Treat unset variables

set -u

Print commands

set -x

Disable an option

set +x

Disable filename expansion

set -f

List all shell variables

set

Set positional parameters

set -- [arg1] [arg2] [arg3]

说明

set is a shell builtin that controls shell options and positional parameters. Options modify shell behavior for scripting, debugging, and interactive use. Use - to enable an option and + to disable it. When invoked without arguments, set displays all shell variables and functions. With -- followed by arguments, it sets the positional parameters ($1, $2, etc.) to the provided values. The combination set -euo pipefail is a common "strict mode" for shell scripts that causes immediate exit on errors, undefined variable references, or pipeline failures—catching bugs early and preventing silent failures.

参数

-a (allexport)
Export all created or modified variables and functions
-b (notify)
Report terminated background job status immediately
-e (errexit)
Exit immediately if a command returns non-zero status
-f (noglob)
Disable filename expansion (globbing)
-h (hashall)
Remember command locations as they are looked up
-k (keyword)
Place all assignment arguments in the environment
-m (monitor)
Enable job control
-n (noexec)
Read commands but do not execute them (syntax check)
-p (privileged)
Enable privileged mode
-t (onecmd)
Exit after reading and executing one command
-u (nounset)
Treat unset variables as an error during expansion
-v (verbose)
Print shell input lines as they are read
-x (xtrace)
Print commands and arguments before execution
-B (braceexpand)
Enable brace expansion (default on)
-C (noclobber)
Prevent overwriting files with output redirection
-E (errtrace)
ERR trap is inherited by shell functions and subshells
-H (histexpand)
Enable ! style history substitution
-P (physical)
Do not resolve symbolic links for commands like cd
-T (functrace)
DEBUG and RETURN traps inherited by functions
-o pipefail
Pipeline returns status of last non-zero command
--
End of options; remaining args become positional parameters

FAQ

What is the set command used for?

set is a shell builtin that controls shell options and positional parameters. Options modify shell behavior for scripting, debugging, and interactive use. Use - to enable an option and + to disable it. When invoked without arguments, set displays all shell variables and functions. With -- followed by arguments, it sets the positional parameters ($1, $2, etc.) to the provided values. The combination set -euo pipefail is a common "strict mode" for shell scripts that causes immediate exit on errors, undefined variable references, or pipeline failures—catching bugs early and preventing silent failures.

How do I run a basic set example?

Run `set -euo pipefail` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a (allexport) do in set?

Export all created or modified variables and functions