Linux command
getopt 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Example
getopt -o vV -l verbose,version -- --version --verbose
Example
getopt -o f: -l file: -- --file=somefile
Example
getopt -o v:: -l verbose:: -- --verbose arg
Example
getopt -o rv::t: -l verbose,source::,target: -- -v --target target
说明
getopt parses command-line options for shell scripts, handling both short (-v) and long (--verbose) option formats. It normalizes option order, handles bundled short options (-abc), and separates options from arguments. The enhanced getopt (util-linux version) supports long options, optional arguments, and proper handling of arguments with spaces. Use getopt -T to test if the enhanced version is available. Output is typically evaluated with eval set -- to update the script's positional parameters with the normalized options.
参数
- -o, --options _SHORT_
- Short options to recognize (single letters). Colon after letter means required argument, double colon means optional.
- -l, --longoptions _LONG_
- Long options to recognize (comma-separated). Colon rules same as short options.
- -n, --name _NAME_
- Name to use in error messages
- -q, --quiet
- Suppress error messages
- -a, --alternative
- Allow long options with single dash
- -T, --test
- Test for enhanced getopt
FAQ
What is the getopt command used for?
getopt parses command-line options for shell scripts, handling both short (-v) and long (--verbose) option formats. It normalizes option order, handles bundled short options (-abc), and separates options from arguments. The enhanced getopt (util-linux version) supports long options, optional arguments, and proper handling of arguments with spaces. Use getopt -T to test if the enhanced version is available. Output is typically evaluated with eval set -- to update the script's positional parameters with the normalized options.
How do I run a basic getopt example?
Run `getopt -o vV -l verbose,version -- --version --verbose` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -o, --options _SHORT_ do in getopt?
Short options to recognize (single letters). Colon after letter means required argument, double colon means optional.