Linux command
emulate 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
emulate
Switch
emulate zsh
Emulate
emulate sh
Emulate
emulate ksh
Reset
emulate -R [zsh|sh|ksh|csh]
Restrict
emulate -L [sh]
Run
emulate [sh] -c '[command]'
List
emulate -lR [ksh]
说明
emulate is a zsh builtin that switches the shell into a compatibility mode resembling another Bourne-family shell. Each mode adjusts a curated set of options governing word splitting, glob behavior, function handling, prompt expansion, and parameter rules so that scripts written for sh, ksh, or csh behave more predictably under zsh. The change applies to the running shell unless -L scopes it to a function. With -R, every settable option is forced to the canonical defaults for the target mode—useful at the top of a script that should run in a clean, predictable environment regardless of the user's interactive setup. The -c form is the safest way to borrow another mode for a snippet: the previous emulation, options, and traps are restored on exit. Functions defined while -c is active acquire sticky emulation, so they continue to evaluate under that mode no matter where they are later called from. This is the mechanism behind the autoloaded zsh/system completion functions. Querying with no arguments returns the current top-level emulation, which can be tested in scripts to gate behavior. Combining -l with -R prints exactly which options would be toggled, making it convenient for inspecting the differences between modes without applying them.
参数
- -L
- Set LOCAL_OPTIONS, LOCAL_PATTERNS, and LOCAL_TRAPS so that the emulation, option changes, and traps revert when the surrounding function returns. Implies a function context.
- -R
- Reset every settable option to its default value for the requested emulation, except for options describing the interactive environment (e.g. ZLE, HIST\_ flags). Without -R, only options that differ between modes are touched.
- -l
- List the options and their values that the current invocation would set, instead of changing the shell state. Combine with -L or -R to scope the listing.
- -c _arg_
- Evaluate _arg_ in the requested emulation, then restore the previous mode and options. Functions defined inside _arg_ become sticky, retaining the temporary emulation when later invoked.
- -M
- When defining a function inside -c, ignore any sticky emulation already attached to the calling context (use the matching one).
- -S
- Counterpart to -M: force the new function to inherit the current sticky emulation regardless of how it was reached.
FAQ
What is the emulate command used for?
emulate is a zsh builtin that switches the shell into a compatibility mode resembling another Bourne-family shell. Each mode adjusts a curated set of options governing word splitting, glob behavior, function handling, prompt expansion, and parameter rules so that scripts written for sh, ksh, or csh behave more predictably under zsh. The change applies to the running shell unless -L scopes it to a function. With -R, every settable option is forced to the canonical defaults for the target mode—useful at the top of a script that should run in a clean, predictable environment regardless of the user's interactive setup. The -c form is the safest way to borrow another mode for a snippet: the previous emulation, options, and traps are restored on exit. Functions defined while -c is active acquire sticky emulation, so they continue to evaluate under that mode no matter where they are later called from. This is the mechanism behind the autoloaded zsh/system completion functions. Querying with no arguments returns the current top-level emulation, which can be tested in scripts to gate behavior. Combining -l with -R prints exactly which options would be toggled, making it convenient for inspecting the differences between modes without applying them.
How do I run a basic emulate example?
Run `emulate` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -L do in emulate?
Set LOCAL_OPTIONS, LOCAL_PATTERNS, and LOCAL_TRAPS so that the emulation, option changes, and traps revert when the surrounding function returns. Implies a function context.