Session and utilities

The getopts command

Walks through a script options (-a, -b value) one at a time.

Syntax

getopts "opciones" variable

Copy-ready examples

$ printf 'getopts "ab:" o\necho listo\n' > p.shScript with getopts
$ bash p.sh -aTry an option

You can try them in the browser terminal: it does not touch your computer.

Typical mistakes

  • A colon after a letter (b:) means that option takes an argument.

Related commands

FAQ

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course