Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-platform getopt for a shell script

I've just found out that getopt is not cross-platform (in particular for FreeBSD and Linux). What is the best workaround for this issue?

like image 416
codeholic Avatar asked Apr 27 '10 14:04

codeholic


1 Answers

Use getopts (with an "s").

According to Bash FAQ 35:

Unless it's the version from util-linux, and you use its advanced mode, never use getopt(1). getopt cannot handle empty arguments strings, or arguments with embedded whitespace. Please forget that it ever existed.

The POSIX shell (and others) offer getopts which is safe to use instead.

like image 81
Dennis Williamson Avatar answered Oct 08 '22 20:10

Dennis Williamson