Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [getopts]

`getopts` is a shell built-in used to parse command line options, while `getopt` is its non-built-in counterpart.

0 votes
1 answer
50 views

Manipulating arguments with OPTIND, OPTARG, getopts, and shift correctly

My small POSIX shell scripts do not usually take any arguments, so this is kind of new to me... The minimal snippet would probably look like this: # default for hotkey variable on top of script is set ...
Vlastimil Burián's user avatar
0 votes
1 answer
41 views

getopt with several `--`

I have a script with this usage: myscript [options] positional-args... -- [fwd-params] Because [options] can have long, or short variants, I like using getopt. But I'm having troubles. I use getopt ...
Stewart's user avatar
  • 14.2k
-1 votes
1 answer
95 views

pass all parameters to another sourced bash script that uses getopts

I am trying to pass all parameters from one script to another. However, there is an issue when the other script is sourced; all parameters are not passing correctly. first.sh: #!/usr/bin/bash while ...
Zero's user avatar
  • 13
2 votes
1 answer
114 views

script arguments in bash

I am working on a a script that need to take two script arguments and use them as variables in the script. I couldn't get this working and unable to find out what I am doing wrong. I see the issue is ...
MO12's user avatar
  • 391
-2 votes
1 answer
298 views

Bash getopts error handling

This is an error I encountered when playing with Getopts. The following is the code snippet I have been playing simply listen to 3 options when executing a script. while getopts "vcn" opt; ...
Dettlaff404's user avatar
2 votes
1 answer
309 views

How bash getopts knows what arguments the call has

If getopts is a bash function, by my understanding, you need to pass $@ - the whole arguments to getopts to let the function know what kind of arguments you have in order to proceed, right? It seems ...
Till's user avatar
  • 193
-1 votes
1 answer
166 views

how to exit immediately with getopts option in bash script

what I tried. #!/bin/bash # set -e echo hello while getopts sr o; do case $o in s) echo "no" yell() { echo "$0: $*" >&2; } die() { yell "...
jian's user avatar
  • 577
-1 votes
1 answer
586 views

What does colon (:) mean for getopts?

I am trying to parse options for my script with getopts and I decided that it would be best to read up on it in the POSIX standard, as it's usually extremely helpful. The DESCRIPTION section is fairly ...
Nicolas Dumitru's user avatar
0 votes
0 answers
40 views

Is there a tool or macro processor capable of compactly expressing BASH CLI interfaces?

I am writing scripts with the following boilerplate CLI pattern: usage(){ echo "$0 yada yada" ... echo } for arg in "$@"; do case "$arg" in '--help|-help') ...
Chris's user avatar
  • 983
0 votes
1 answer
95 views

Multichar options in Unix sysytems

I have a usecase wherein I need to support multiple character option. Currently I am using single character option for getopts. Multi-character option is desirable.What's the best way of doing this? I ...
Vatsal A Mehta's user avatar
-1 votes
1 answer
329 views

How to check for passed options using getopts in a POSIX shell? (Report, count, and discard.)

How to check for passed options using getopts (man page) in a POSIX shell? (Report, count, and discard.) My idea is this: Many of my scripts take no options (not even -h for help, I am transitioning ...
Vlastimil Burián's user avatar
-2 votes
1 answer
799 views

The workings of getopts OPTIND

I want to understand how OPTIND works by getopts. If I want to skip the first few positional arguments, how should I set up OPTIND exactly ? And because OPTIND is not reset automatically, I need to ...
Vera's user avatar
  • 1
1 vote
0 answers
34 views

What the... Variable String [duplicate]

I believe I am running into a scenario where escaping quotes and utilizing variables as strings is causing some issues and after some hours of troubleshooting this isn't seeming to come to a ...
Nathan's user avatar
  • 23
3 votes
1 answer
247 views

getopts for more than 4 parser arguments

I want to parse multiple arguments using getopts in a bash script using the code below. while getopts b:B:m:M:T flag do case "${flag}" in b) rbmin=${OPTARG};; B) rbmax=${...
Amit Kr. Ratewal's user avatar
2 votes
1 answer
5k views

How to mix plain arguments with flagged arguments in bash scripting?

for instance, gcc accepts the input file without any flag and the output file with the -o flag in: gcc input.c -o output.out or gcc -o output.out input.c I am creating a random password generator bash ...
Shriram's user avatar
  • 216

15 30 50 per page
1
2 3 4 5
8