Skip to main content

Questions tagged [getopts]

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

Filter by
Sorted by
Tagged with
0 votes
1 answer
43 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
90 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
111 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
281 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
305 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
156 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
566 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
319 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
781 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
243 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
3 votes
2 answers
2k views

Can getopts arguments be combined with other input?

I am writing a script that would take a string as input along with other options the user can select by using arguments as indicators. So in other words, something like this: ./script "My input ...
Isaac D. Cohen's user avatar
1 vote
2 answers
392 views

identify if present and remove specific argument from shell args

I have to check if particular argument lets say 'java8' is present in my shell arguments to script and if it is present remove it . Also I want it to be stored in some other variable , but want it to ...
user3118280's user avatar
0 votes
2 answers
822 views

Use getopt to directly retrieve option value

I am not very experienced with getopt, but in all codes I have seen, it is only being used to reorder the program arguments and then use switch/case to decode which options and arguments are being ...
Ferazhu's user avatar
0 votes
1 answer
1k views

Bash script with subcommand and long options only

How can I use getopt or getopts with subcommands and long options, not with short options? I know how to implement short and long options with getopts. Solutions that I've found so far are using ...
Mubin Icyer's user avatar
4 votes
1 answer
4k views

How to use getopts in bash

I am new to bash scripting and I am trying to write a script with getopts so that when script.sh -sp is is invoked the URL and column and row count is printed out. And when script.sh -r option is ...
Olaola's user avatar
  • 55
0 votes
1 answer
1k views

getopts with double colons for optional values

Does getopts not accept optional arguments for an option using double colons (e.g. using shortopts="t::")?
Vera's user avatar
  • 1
2 votes
2 answers
13k views

Handling long-options with getopts

I am parsing options with getopts but would like to handle long-options as well. print-args () { title="$1" ; shift printf "\n%s\n" "${title}: \$@:" for arg in "$@...
Vera's user avatar
  • 1
1 vote
1 answer
282 views

Ensuring only 1 mandatory parameter is passed to script

I am writing a shell script and new to getopts for parameter parsing. What I have are 1 optional and 2 mandatory parameters and what I want to do is ensure that only one mandatory parameter is passed. ...
aika's user avatar
  • 13
1 vote
3 answers
6k views

Detecting getopts `--` (double dash) to issue a message

I am using the customary way of using getopts through a variable named arg. I can capture the option names as follows. Is it possible to detect the moment getopts reaches "--" so that I ...
Alta's user avatar
  • 31
-2 votes
2 answers
2k views

Setting variables using getopts [duplicate]

I would like to use getopts for parsing arguments to bash functions. while getopts ${shortopts} arg; do But am encountering problems in setting variable values. Calling myfunc -v8 or myfunc -v 8 ...
Isabel's user avatar
  • 79
-2 votes
1 answer
743 views

getopt long and short options

Does getopt require a short option for every defined long option? In other words, can I have a long option without being required to define an equivalent short option.
Pietru's user avatar
  • 393
3 votes
2 answers
4k views

bash script with optional input arguments using getopt

I have copied code from tutorialspoint's getopt article and got the following script to work (sort of): ##argument_script.sh VARS=`getopt -o i::o:: --long input::,output:: -- "$@"` eval set ...
Jonas Olsen's user avatar
0 votes
0 answers
55 views

Shell script getopts weird behaviour args not parsing

I'm trying a simple shell script to get the arguments passed by the user, but cannot seem to find my error. Sometimes it works, but mostly not (weird). helpFunc() { echo "" ...
Omar Omeiri's user avatar
3 votes
2 answers
1k views

shell getopt with help argument [closed]

I'm preparing a script with getopt. I would like to add the help section. So if they use --help or -h it should execute the function (just print the instructions) and return it. Sample code: log_type=...
TheDataGuy's user avatar
13 votes
1 answer
42k views

Bash getopts, mandatory arguments

I am working with a pretty simple bash script, but I am facing a problem which I can't resolve: I have myscript.sh with three parameters, -u, -h, and -p. Both -u and -h are mandatory, needed for the ...
Learner33's user avatar
  • 133
3 votes
1 answer
2k views

Using command-line parameters as destination for cp and mv in bash script

I am attempting to do copy a file (or rename a file) by running the script with flags/parameters to give both the source and the destination file name: #!/bin/bash/ while getopts s:d flag do ...
florinVlad's user avatar
0 votes
2 answers
7k views

Processing optional arguments with getopts in bash

I have a bash script which processes an input file with optional arguments. The script looks like this #!/bin/bash while getopts a:b:i: option do case "${option}" in a) arg1=${OPTARG};; b) ...
220284's user avatar
  • 141
4 votes
1 answer
2k views

getopts gets no arguments when function is called inside while loop

I am trying to call a function in a while loop passing some arguments. However, getopts can only get the arguments for the first call. Here's a minimal example: function add_all_external_services() { ...
leoschet's user avatar
  • 233
0 votes
0 answers
95 views

when don't I give the argument to option, option is not working

example code: function purge() { delete_dir; case $purge_arg in --static) delete_static_dir; ;; esac } while getopts ":nrp:h" opt; do case ${opt} in h ) ...
rojen's user avatar
  • 184
1 vote
1 answer
2k views

getopt - push default value if the argument is not provided

Im following this guide to using getopt. #!/bin/bash ALPHA=test BETA=test CHARLIE=test DELTA=test usage() { echo "Usage: alphabet [ -a | --alpha ] [ -b | --beta ] [ -c |...
TheDataGuy's user avatar
1 vote
0 answers
393 views

getopt - how to avoid read next flag as argument?

When I use getopt and pass the required flags with empty argument, it reads the next flag as an argument. Need assistance to add some sort of check to distinguish between flag containing dash (-) and ...
Riz's user avatar
  • 59
3 votes
2 answers
2k views

Parsing script arguments after getopts

I have a script where I've implemented switches using getopts. However, I'm having trouble referencing the next argument. My script is for backporting a backup of our website on a local development ...
user394's user avatar
  • 14.5k
2 votes
2 answers
3k views

Proper way to code dry run option without having to repeat myself?

I'm coding a script that goes and searches for files on a remote server and transfers them back to my local computer. I want to be able to do a dry run first, so I know which files I'm bringing back. ...
dylanjm's user avatar
  • 201
2 votes
2 answers
2k views

How to correctly identify the order of parameters?

This is my assignment. The task is to output n longest lines from the input file(s). If there is no argument for n, the the default of value n is 5. If there is no files in the parameter, the standard ...
amV's user avatar
  • 75
0 votes
2 answers
1k views

Set shell variable then invoke subprocess fails

I was playing with getopts, which has a shell variable OPTERR that influences its behavior. I wanted to change the value of OPTERR and invoke getopts on the same line, to affect the behavior of ...
cycollins's user avatar
  • 151
3 votes
1 answer
699 views

Unexpected behavior from getopts

Considering: #!/bin/sh while getopts ":h" o; do case "$o" in h ) "Usage: sh $(basename "$0") -h Displays help message sh $(basename "$0") arg Outputs ... where: -...
Erwann's user avatar
  • 667
1 vote
1 answer
2k views

Bash getopts take more positional parameters after the first argument

So I don't know how to make getopts so that it takes every positional parameter after the argument. What I mean by this, is the following: this is my example code: #!/bin/bash while getopts 'a:b:' ...
user323587's user avatar
0 votes
3 answers
3k views

Call a function with an argument inside a function that needs another argument

I don't know how else to word this so I'll do my best to explain. I have a function main() { if [[ "$1" = "-f" ]] then ($sendFirstEmail) "$1" elif [[ "$1" = "...
Michael 's user avatar
1 vote
1 answer
867 views

getopts in function that is called more than once in a script, getopts doesn't detect any opts after 1st function call [duplicate]

I have read the getopts man page and am still not sure about this use case. getopts is not detecting any available options the second time a function is called in the same script. As you can see ...
Ryan Mahaffey's user avatar
1 vote
1 answer
2k views

How do I pass long-style command line options to a bash script using getopt?

I have this script that I have cobbled together from bits of lore I've gleaned from googling: #!/usr/bin/env bash usage() { declare -r script_name=$(basename "$0") echo """ Usage: "${...
braveterry's user avatar
-1 votes
1 answer
542 views

Bash Script Wind Chill using getopt and bc

I am now getting the correct output, however it is not scaling to only 3 places. Anyone know how to do that? function windchill { to=32 v=10 vel=`echo 'e(l('$v')*0.16)' | bc -l` w=$(...
Trinity Zamrzla's user avatar
4 votes
1 answer
6k views

getopts does not seem to work

I am trying to run the following script using getopts to parse the options but it does not seem to work: #!/bin/bash set -x echo $@ while getopts "rf" opt do case "${opt}" in r) ropt=${...
trikelef's user avatar
  • 411
1 vote
1 answer
3k views

shift in getopts loop - clarification needed

I would be glad if someone clarified the need to use shift in this simple parser code: while getopts ":hp:" option do case "${option}" in p) some_parameter=$...
Vlastimil Burián's user avatar
0 votes
2 answers
319 views

Questions about understanding a template of using bash's getopts

A stackoverflow post has a template for handling command line arguments. Does the test [ $# == 0 ] mean that a bash script shouldn't be run without any argument? As a template, I think that scripts ...
Tim's user avatar
  • 103k
-1 votes
2 answers
1k views

In a shell script, process options like -a -b but leave --abc alone?

So I have a shell script that does some preparation and then runs a utility. The preparation can be affected by switches like -a or -n. The contents of the command line that are not such switches are ...
Mikhail Ramendik's user avatar