• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

getopt Command Examples in Linux

by Deepika

getopt is a command line utility that is used to parse command line arguments passed to a program. It is typically used in Unix-like operating systems and is available on Linux, macOS and other Unix-like systems.

getopt is useful when you want to write a program that accepts various options and arguments from the command line. It can be used to parse both short and long options (options that start with one or two dashes, respectively) and to handle options that take arguments. The syntax for using getopt is as follows:

# getopt options [argument ...]

where options is a string containing the list of options that the program accepts, and argument is the list of arguments passed to the program.

The options string is a list of characters that represent the short options that the program accepts. Each character is followed by a colon if the option takes an argument. For example, if a program accepts the options -a, -b and -c which takes arguments, options string will be “abc:”

For Long options, the options string is a list of characters that represent the short options that the program accepts, each character can be followed by colon if the option takes an argument.

The getopt command returns the next option character in the option string each time it is called. The program can then process the option and its argument (if it takes one). The getopt command also keeps track of the position in the argument list, so that the program can access the non-option arguments after all the options have been processed.

getopt Command Examples

1. Parse optional `verbose`/`version` flags with shorthands:

# getopt --options vV --longoptions verbose,version -- --version --verbose

2. Add a `–file` option with a required argument with shorthand `-f`:

# getopt --options f: --longoptions file: -- --file=somefile

3. Add a `–verbose` option with an optional argument with shorthand `-v`, and pass a non-option parameter `arg`:

# getopt --options v:: --longoptions verbose:: -- --verbose arg

4. Accept a `-r` and `–verbose` flag, a `–accept` option with an optional argument and add a `–target` with a required argument option with shorthands:

# getopt --options rv::s::t: --longoptions verbose,source::,target: -- -v --target target

Filed Under: Linux

Some more articles you might also be interested in …

  1. dpkg: command not found
  2. synopkg Command Examples in Linux
  3. CentOS / RHEL : How To Shrink LVM Root File System
  4. ctrlaltdel Command Examples in Linux
  5. feedreader Command Examples in Linux
  6. iwctl: command not found
  7. localectl: command not found
  8. chkconfig: command not found
  9. errno: Look up errno names and descriptions
  10. gnatmake Command Examples

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright