JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
man pages section 1: User Commands     Oracle Solaris 11.1 Information Library
search filter icon
search icon

Document Information

Preface

Introduction

User Commands

acctcom(1)

adb(1)

addbib(1)

admin(1)

alias(1)

allocate(1)

amt(1)

appcert(1)

apptrace(1)

apropos(1)

ar(1)

arch(1)

as(1)

asa(1)

at(1)

atq(1)

atrm(1)

audioconvert(1)

audioctl(1)

audioplay(1)

audiorecord(1)

audiotest(1)

auths(1)

auto_ef(1)

awk(1)

banner(1)

basename(1)

basename(1B)

batch(1)

bc(1)

bdiff(1)

bfs(1)

bg(1)

biff(1B)

break(1)

builtin(1)

cal(1)

calendar(1)

case(1)

cat(1)

cd(1)

cdc(1)

cdrw(1)

chdir(1)

checkeq(1)

checknr(1)

chgrp(1)

chkey(1)

chmod(1)

chown(1)

chown(1B)

ckdate(1)

ckgid(1)

ckint(1)

ckitem(1)

ckkeywd(1)

ckpath(1)

ckrange(1)

ckstr(1)

cksum(1)

cktime(1)

ckuid(1)

ckyorn(1)

clear(1)

cmp(1)

col(1)

comb(1)

comm(1)

command(1)

compress(1)

continue(1)

cp(1)

cpio(1)

cpp(1)

cputrack(1)

crle(1)

crontab(1)

csh(1)

csplit(1)

ct(1C)

ctags(1)

ctrun(1)

ctstat(1)

ctwatch(1)

cu(1C)

cut(1)

date(1)

dc(1)

deallocate(1)

decrypt(1)

delta(1)

deroff(1)

df(1B)

dhcpinfo(1)

diff(1)

diff3(1)

diffmk(1)

digest(1)

digestp(1)

dircmp(1)

dirname(1)

dirs(1)

dis(1)

disown(1)

dispgid(1)

dispuid(1)

dos2unix(1)

dpost(1)

du(1)

du(1B)

dump(1)

dumpcs(1)

dumpkeys(1)

echo(1)

echo(1B)

ed(1)

edit(1)

egrep(1)

eject(1)

elfdump(1)

elfedit(1)

elffile(1)

elfsign(1)

elfwrap(1)

encrypt(1)

enhance(1)

env(1)

eqn(1)

errange(1)

errdate(1)

errgid(1)

errint(1)

erritem(1)

error(1)

errpath(1)

errstr(1)

errtime(1)

erruid(1)

erryorn(1)

eval(1)

ex(1)

exec(1)

exit(1)

expand(1)

export(1)

exportfs(1B)

expr(1)

expr(1B)

exstr(1)

factor(1)

false(1)

fastboot(1B)

fasthalt(1B)

fc(1)

fg(1)

fgrep(1)

file(1)

file(1B)

filebench(1)

filep(1)

filesync(1)

filofaxp(1)

find(1)

finger(1)

fmt(1)

fmtmsg(1)

fold(1)

for(1)

foreach(1)

franklinp(1)

from(1B)

ftp(1)

function(1)

gcore(1)

gencat(1)

geniconvtbl(1)

genmsg(1)

get(1)

getconf(1)

getfacl(1)

getlabel(1)

getopt(1)

getoptcvt(1)

getopts(1)

gettext(1)

gettxt(1)

getzonepath(1)

glob(1)

goto(1)

gprof(1)

grep(1)

groups(1)

groups(1B)

grpck(1B)

hash(1)

hashcheck(1)

hashmake(1)

hashstat(1)

head(1)

helpdate(1)

helpgid(1)

helpint(1)

helpitem(1)

helppath(1)

helprange(1)

helpstr(1)

helptime(1)

helpuid(1)

helpyorn(1)

hist(1)

history(1)

hostid(1)

hostname(1)

i386(1)

i486(1)

iconv(1)

if(1)

indxbib(1)

install(1B)

ipcrm(1)

ipcs(1)

isainfo(1)

isalist(1)

jobs(1)

join(1)

jsh(1)

kbd(1)

kdestroy(1)

keylogin(1)

keylogout(1)

kill(1)

kinit(1)

klist(1)

kmdb(1)

kmfcfg(1)

kpasswd(1)

krb5-config(1)

ksh(1)

ksh88(1)

ksh93(1)

ktutil(1)

kvno(1)

lari(1)

last(1)

lastcomm(1)

ld(1)

ldapadd(1)

ldapdelete(1)

ldaplist(1)

ldapmodify(1)

ldapmodrdn(1)

ldapsearch(1)

ldd(1)

ld.so.1(1)

let(1)

lex(1)

lgrpinfo(1)

limit(1)

line(1)

list_devices(1)

listusers(1)

llc2_autoconfig(1)

llc2_config(1)

llc2_stats(1)

ln(1)

ln(1B)

loadkeys(1)

locale(1)

localedef(1)

logger(1)

logger(1B)

login(1)

logname(1)

logout(1)

look(1)

lookbib(1)

lorder(1)

ls(1)

ls(1B)

m4(1)

mac(1)

mach(1)

machid(1)

madv.so.1(1)

mail(1)

Mail(1B)

mail(1B)

mailcompat(1)

mailp(1)

mailq(1)

mailstats(1)

mailx(1)

make(1S)

makekey(1)

man(1)

mconnect(1)

mcs(1)

mdb(1)

mesg(1)

mkdir(1)

mkmsgs(1)

mkstr(1B)

mktemp(1)

moe(1)

more(1)

mp(1)

mpss.so.1(1)

msgcc(1)

msgcpp(1)

msgcvt(1)

msgfmt(1)

msggen(1)

msgget(1)

mt(1)

mv(1)

nawk(1)

nc(1)

ncab2clf(1)

ncakmod(1)

neqn(1)

netcat(1)

newform(1)

newgrp(1)

newsp(1)

newtask(1)

nice(1)

nl(1)

nm(1)

nohup(1)

notify(1)

nroff(1)

od(1)

on(1)

onintr(1)

optisa(1)

pack(1)

packagemanager(1)

page(1)

pagesize(1)

pargs(1)

passwd(1)

paste(1)

patch(1)

pathchk(1)

pax(1)

pcat(1)

pcred(1)

perl(1)

pfbash(1)

pfcsh(1)

pfexec(1)

pfiles(1)

pfksh(1)

pflags(1)

pfsh(1)

pftcsh(1)

pfzsh(1)

pg(1)

pgrep(1)

pkcs11_inspect(1)

pkg(1)

pkgdepend(1)

pkgdiff(1)

pkgfmt(1)

pkginfo(1)

pkglint(1)

pkgmerge(1)

pkgmk(1)

pkgmogrify(1)

pkgparam(1)

pkgproto(1)

pkgrecv(1)

pkgrepo(1)

pkgsend(1)

pkgsign(1)

pkgtrans(1)

pkill(1)

pklogin_finder(1)

pktool(1)

plabel(1)

pldd(1)

plgrp(1)

plimit(1)

pmadvise(1)

pmap(1)

pm-updatemanager(1)

popd(1)

ppgsz(1)

ppriv(1)

pr(1)

praliases(1)

prctl(1)

preap(1)

print(1)

printenv(1B)

printf(1)

priocntl(1)

proc(1)

prof(1)

profiles(1)

projects(1)

prs(1)

prt(1)

prun(1)

ps(1)

ps(1B)

psig(1)

pstack(1)

pstop(1)

ptime(1)

ptree(1)

pushd(1)

pvs(1)

pwait(1)

pwd(1)

pwdx(1)

radadrgen(1)

ranlib(1)

rcapstat(1)

rcp(1)

read(1)

readonly(1)

red(1)

refer(1)

regcmp(1)

rehash(1)

remote_shell(1)

remsh(1)

renice(1)

repeat(1)

reset(1B)

return(1)

rksh(1)

rksh88(1)

rlogin(1)

rm(1)

rmail(1)

rmdel(1)

rmdir(1)

rmformat(1)

rmmount(1)

rmumount(1)

roffbib(1)

roles(1)

rpcgen(1)

rpm2cpio(1)

rsh(1)

runat(1)

rup(1)

rup(1C)

ruptime(1)

rusage(1B)

rusers(1)

rwho(1)

sact(1)

sar(1)

sccs(1)

sccs-admin(1)

sccs-cdc(1)

sccs-comb(1)

sccs-delta(1)

sccsdiff(1)

sccs-get(1)

sccs-help(1)

sccshelp(1)

sccs-prs(1)

sccs-prt(1)

sccs-rmdel(1)

sccs-sact(1)

sccs-sccsdiff(1)

sccs-unget(1)

sccs-val(1)

scp(1)

script(1)

sdiff(1)

sed(1)

sed(1B)

select(1)

set(1)

setenv(1)

setfacl(1)

setlabel(1)

setpgrp(1)

settime(1)

sftp(1)

sh(1)

shcomp(1)

shell_builtins(1)

shift(1)

shutdown(1B)

size(1)

sleep(1)

soelim(1)

sort(1)

sortbib(1)

sotruss(1)

source(1)

sparc(1)

spell(1)

spellin(1)

split(1)

srchtxt(1)

ssh(1)

ssh-add(1)

ssh-agent(1)

ssh-http-proxy-connect(1)

ssh-keygen(1)

ssh-keyscan(1)

ssh-socks5-proxy-connect(1)

stop(1)

strchg(1)

strconf(1)

strings(1)

strip(1)

stty(1)

stty(1B)

sum(1)

sum(1B)

sun(1)

suspend(1)

svcprop(1)

svcs(1)

switch(1)

symorder(1)

sys-suspend(1)

sysV-make(1)

t300(1)

t300s(1)

t4014(1)

t450(1)

tabs(1)

tail(1)

talk(1)

tar(1)

tbl(1)

tcopy(1)

tee(1)

tek(1)

telnet(1)

test(1)

test(1B)

tftp(1)

time(1)

timemanp(1)

times(1)

timesysp(1)

timex(1)

tip(1)

touch(1)

touch(1B)

tplot(1)

tput(1)

tr(1)

tr(1B)

trap(1)

troff(1)

true(1)

truss(1)

tset(1B)

tsort(1)

tty(1)

type(1)

typeset(1)

ul(1)

ulimit(1)

umask(1)

unalias(1)

uname(1)

uncompress(1)

unexpand(1)

unget(1)

unhash(1)

unifdef(1)

uniq(1)

units(1)

unix2dos(1)

unlimit(1)

unpack(1)

unset(1)

unsetenv(1)

until(1)

updatehome(1)

uptime(1)

userattr(1)

users(1B)

uucp(1C)

uudecode(1C)

uuencode(1C)

uuglist(1C)

uulog(1C)

uuname(1C)

uupick(1C)

uustat(1C)

uuto(1C)

uux(1C)

vacation(1)

val(1)

valdate(1)

valgid(1)

valint(1)

valpath(1)

valrange(1)

valstr(1)

valtime(1)

valuid(1)

valyorn(1)

vc(1)

vedit(1)

ver(1)

vgrind(1)

vi(1)

view(1)

vipw(1B)

volcheck(1)

volrmmount(1)

w(1)

wait(1)

wc(1)

what(1)

whatis(1)

whence(1)

whereis(1B)

which(1)

while(1)

who(1)

whoami(1B)

whocalls(1)

whois(1)

write(1)

xargs(1)

xgettext(1)

xstr(1)

yacc(1)

yes(1)

ypcat(1)

ypmatch(1)

yppasswd(1)

ypwhich(1)

zcat(1)

zlogin(1)

zonename(1)

zonestat(1)

getopts

- parse utility options

Synopsis

/usr/bin/getopts optstring name [arg...]

sh

getopts optstring name [argument]...

ksh88

getopts optstring name [arg]...

ksh

getopts [-a name] optstring name [arg]...

Description

/usr/bin/getopts

The getopts utility can be used to retrieve options and option-arguments from a list of parameters.

Each time it is invoked, the getopts utility places the value of the next option in the shell variable specified by the name operand and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell is invoked, OPTIND is initialized to 1.

When the option requires an option-argument, the getopts utility places it in the shell variable OPTARG. If no option was found, or if the option that was found does not have an option-argument, OPTARG is unset.

If an option character not contained in the optstring operand is found where an option character is expected, the shell variable specified by name is set to the question-mark ( ? ) character. In this case, if the first character in optstring is a colon (:), the shell variable OPTARG is set to the option character found, but no output is written to standard error; otherwise, the shell variable OPTARG is unset and a diagnostic message is written to standard error. This condition is considered to be an error detected in the way arguments were presented to the invoking application, but is not an error in getopts processing.

If an option-argument is missing:

When the end of options is encountered, the getopts utility exits with a return value greater than zero; the shell variable OPTIND is set to the index of the first non-option-argument, where the first - - argument is considered to be an option-argument if there are no other non-option-arguments appearing before it, or the value $# + 1 if there are no non-option-arguments; the name variable is set to the question-mark character. Any of the following identifies the end of options: the special option - -, finding an argument that does not begin with a -, or encountering an error.

The shell variables OPTIND and OPTARG are local to the caller of getopts and are not exported by default.

The shell variable specified by the name operand, OPTIND and OPTARG affect the current shell execution environment.

If the application sets OPTIND to the value 1, a new set of parameters can be used: either the current positional parameters or new arg values. Any other attempt to invoke getopts multiple times in a single shell execution environment with parameters (positional parameters or arg operands) that are not the same in all invocations, or with an OPTIND value modified to be a value other than 1, produces unspecified results.

sh

getopts is a built-in Bourne shell command used to parse positional parameters and to check for valid options. See sh(1). It supports all applicable rules of the command syntax standard (see Rules 3-10, Intro(1)). It should be used in place of the getopt command.

optstring must contain the option letters the command using getopts recognizes. If a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space.

Each time it is invoked, getopts places the next option in the shell variable name and the index of the next argument to be processed in the shell variable OPTIND. Whenever the shell or a shell script is invoked, OPTIND is initialized to 1.

When an option requires an option-argument, getopts places it in the shell variable OPTARG.

If an illegal option is encountered, ? is placed in name.

When the end of options is encountered, getopts exits with a non-zero exit status. The special option can be used to delimit the end of the options.

By default, getopts parses the positional parameters. If extra arguments (argument . . .) are specified on the getopts command line, getopts parses them instead.

/usr/lib/getoptcvt reads the shell script in filename, converts it to use getopts instead of getopt, and writes the results on the standard output.

So that all new commands adhere to the command syntax standard described in Intro(1), they should use getopts or getopt to parse positional parameters and check for options that are valid for that command.

getopts prints an error message on the standard error when it encounters an option letter not included in optstring.

Although the following command syntax rule (see Intro(1)) relaxations are permitted under the current implementation, they should not be used because they can not be supported in future releases of the system. As in the EXAMPLES section below, -a and -b are options, and the option -o requires an option-argument.

The following example violates Rule 5: options with option-arguments must not be grouped with other options:

example% cmd -aboxxx filename

The following example violates Rule 6: there must be white space after an option that takes an option-argument:

example% cmd -ab oxxx filename

Changing the value of the shell variable OPTIND or parsing different sets of arguments can lead to unexpected results.

ksh88

Checks arg for legal options. If arg is omitted, the positional parameters are used. An option argument begins with a + or a -. An option not beginning with + or - or the argument ends the options. optstring contains the letters that getopts recognizes. If a letter is followed by a :, that option is expected to have an argument. The options can be separated from the argument by blanks.

getopts places the next option letter it finds inside variable name each time it is invoked with a + prepended when arg begins with a +. The index of the next arg is stored in OPTIND. The option argument, if any, gets stored in OPTARG.

A leading : in optstring causes getopts to store the letter of an invalid option in OPTARG, and to set name to ? for an unknown option and to : when a required option is missing. Otherwise, getopts prints an error message. The exit status is non-zero when there are no more options.

getopts supports both traditional single-character short options and long options defined by Sun's Command Line Interface Paradigm (CLIP).

Each long option is an alias for a short option and is specified in parentheses following its equivalent short option. For example, you can specify the long option file as an alias for the short option f using the following script line:

getopts "f(file)" opt

Precede long options on the command line with -- or ++. In the example above, --file on the command line would be the equivalent of -f, and ++file on the command line would be the equivalent of +f.

Each short option can have multiple long option equivalents, although this is in violation of the CLIP specification and should be used with caution. You must enclose each long option equivalent parentheses, as follows:

getopts "f:(file)(input-file)o:(output-file)"

In the above example, both --file and --input-file are the equivalent of -f, and --output-file is the equivalent of -o.

The variable name is always set to a short option. When a long option is specified on the command line, name is set to the short-option equivalent.

For a further discussion of the Korn shell's getopts built-in command, see the previous discussion in the Bourne shell (sh) section of this manpage.

ksh

The getopts utility can be used to retrieve options and arguments from a list of arguments specified by args or the positional parameters if arg is omitted. It can also generate usage messages and a manual page for the command based on the information in optstring.

Each time it is invoked, the getopts utility places the value of the next option in the shell variable specified by the name operand and the index of the next argument to be processed in the shell variable OPTIND. When the shell is invoked OPTIND is initialized to 1. When an option requires or permits an option argument, getopts places the option argument in the shell variable OPTARG. Otherwise OPTARG is set to 1 when the option is set and 0 when the option is unset.

The optstring string consists of alphanumeric characters, the special characters +, -, ?, :, and SPACE or character groups enclosed in [...]. Character groups can be nested in {...}. Outside of a [...] group, a single NEWLINE followed by zero or more blanks is ignored. One or more blank lines separate the options from the command argument synopsis.

Each [...] group consists of an optional label, optional attributes separated by :, and an optional description string following ?. The characters from the ? to the end of the next ] are ignored for option parsing and short usage messages. They are used for generating verbose help or man pages. The : character can not appear in the label. The ? character must be specified as ?? in the label and the ] character must be specified as ]] in the description string. Text between two \b (backspace) characters indicates that the text should be emboldened when displayed. Text between two \a (bell) characters indicates that the text should be emphasized or italicized when displayed. Text between two \v (vertical tab) characters indicates that the text should displayed in a fixed-width font. Text between two \f (form feed) characters is replaced by the output from the shell function whose name is that of the enclosed text.

All output from this interface is written to the standard error.

There are several group types:

If the leading character of optstring is +, arguments beginning with + are also be considered options.

A leading : character or a : following a leading + in optstring affects the way errors are handled. If an option character or longname argument not specified in optstring is encountered when processing options, the shell variable whose name is name is set to the ? character. The shell variable OPTARG is set to the character found. If an option argument is missing or has an invalid value, then name is set to the : character and the shell variable OPTARG is set to the option character found. Without the leading :, name is set to the ? character, OPTARG is unset, and an error message is written to standard error when errors are encountered.

The end of options occurs when:

  1. The special argument -- is encountered.

  2. An argument that does not begin with a - is encountered.

  3. A help argument is specified.

  4. An error is encountered.

If OPTIND is set to the value 1, a new set of arguments can be used.

getopts can also be used to generate help messages containing command usage and detailed descriptions. Specify args as:

-?

Use this to generate a usage synopsis.

--??

Use this to generate a verbose usage message.

--??man

Use this to generate a formatted manual page.

--??api

Use this to generate an easy to parse usage message.

--??html

Use this to generate a man page in html format.

--??nroff

Use this to generate a man page in nroff format.

--??usage

Use this to list the current optstring.

--???name

Use this to list version=n, where n is greater than 0, if the option name is recognized by getopts.

When the end of options is encountered, getopts exits with a non-zero return value and the variable OPTIND is set to the index of the first non-option argument.

Options

ksh

The following options are supported by ksh:

-a name

Use name instead of the command name in usage messages.

Operands

The following operands are supported:

optstring

A string containing the option characters recognised by the utility invoking getopts. If a character is followed by a colon, the option is expected to have an argument, which should be supplied as a separate argument. Applications should specify an option character and its option-argument as separate arguments, but getopts interprets the characters following an option character requiring arguments as an argument whether or not this is done. An explicit null option-argument need not be recognised if it is not supplied as a separate argument when getopts is invoked; see getopt(3C). The characters question-mark (?) and colon (:) must not be used as option characters by an application. The use of other option characters that are not alphanumeric produces unspecified results. If the option-argument is not supplied as a separate argument from the option character, the value in OPTARG is stripped of the option character and the -. The first character in optstring determines how getopts behaves if an option character is not known or an option-argument is missing.

name

The name of a shell variable that is set by the getopts utility to the option character that was found.

The getopts utility by default parses positional parameters passed to the invoking shell procedure. If args are specified, they are parsed instead of the positional parameters.

Usage

Since getopts affects the current shell execution environment, it is generally provided as a shell regular built-in. If it is called in a subshell or separate utility execution environment, such as one of the following:

      (getopts abc value "$@")
       nohup getopts ...
       find . -exec getopts ... \;

it does not affect the shell variables in the caller's environment.

Notice that shell functions share OPTIND with the calling shell even though the positional parameters are changed. Functions that want to use getopts to parse their arguments usually want to save the value of OPTIND on entry and restore it before returning. However, there are cases when a function wants to change OPTIND for the calling shell.

Examples

Example 1 Parsing and Displaying Arguments

The following example script parses and displays its arguments:

aflag=
bflag=
while getopts ab: name
do
     case $name in
     a)      aflag=1;;
     b)      bflag=1
             bval="$OPTARG";;
     ?)     printf "Usage: %s: [-a] [-b value] args\n"  $0
            exit 2;;
     esac
done
if [ ! -z "$aflag" ]; then
   printf "Option -a specified\n"
fi
if [ ! -z "$bflag" ]; then
     printf 'Option -b "%s" specified\n' "$bval"
fi
shift $(($OPTIND - 1))
printf "Remaining arguments are: %s\n" "$*"

Example 2 Processing Arguments for a Command with Options

The following fragment of a shell program processes the arguments for a command that can take the options -a or -b. It also processes the option -o, which requires an option-argument:

while getopts abo: c
do
      case $c in
     a | b)   FLAG=$c;;
     o)       OARG=$OPTARG;;
     \?)      echo $USAGE
        exit 2;;
     esac
done
shift `expr $OPTIND - 1`

Example 3 Equivalent Code Expressions

This code example accepts any of the following as equivalent:

cmd -a -b -o "xxx z yy" filename
cmd -a -b -o "xxx z yy" -- filename
cmd -ab -o xxx,z,yy filename
cmd -ab -o "xxx z yy" filename
cmd -o xxx,z,yy -b -a filename

Environment Variables

See environ(5) for descriptions of the following environment variables that affect the execution of getopts: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES, and NLSPATH.

OPTIND

This variable is used by getopts as the index of the next argument to be processed.

OPTARG

This variable is used by getopts to store the argument if an option is using arguments.

Exit Status

The following exit values are returned:

0

An option, specified or unspecified by optstring, was found.

>0

The end of options was encountered or an error occurred.

ksh

The following exit values are returned by ksh:

0

A specified option was found.

1

An end of options was encountered.

2

A usage or information message was generated.

Attributes

See attributes(5) for descriptions of the following attributes:

/usr/bin/getopts, sh, ksh88

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Availability
system/core-os
Interface Stability
Committed
Standard

ksh

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Availability
system/core-os
Interface Stability
Uncommitted

See Also

Intro(1), getoptcvt(1), ksh(1), ksh88(1), ps(1), sh(1), getopt(3C), attributes(5), environ(5), standards(5)

Diagnostics

Whenever an error is detected and the first character in the optstring operand is not a colon (:), a diagnostic message is written to standard error with the following information in an unspecified format: