1

Say I have a bash script with two optional arguments

How would I be able to run the script providing an input for the second argument, but not the first argument?

lit
  • 13
  • 3
  • 2
    Please add an example of what you would like to do and what you are trying so far https://stackoverflow.com/help/how-to-ask – Will Barnwell Sep 27 '17 at 02:12
  • You may need to use `getopts` and your answer is probably covered here https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash – Will Barnwell Sep 27 '17 at 02:13
  • Possible duplicate of [How do I parse command line arguments in Bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) – glennsl Sep 27 '17 at 02:22
  • Yes, please explain. The shell will always provide *all* arguments to your script. You do not have to use the first, but it's not like you can put a certain number of spaces before an argument and have it counted as the next. – David C. Rankin Sep 27 '17 at 02:23
  • So lets say I have a script that converts the capitalization of a string, i.e ./case [arg1] [arg2], where arg1 is either "lowercase" or "uppercase" and arg2 is any string. If arg1 isn't provided, then it will just echo arg2. How would I run this script in terminal with nothing provided for arg1, and "abcdefgh" for arg2? – lit Sep 27 '17 at 02:48
  • `unusedVar="$1"; usefulVar="$2"` ? Good luck. – shellter Sep 27 '17 at 03:31

1 Answers1

5

The shell's argument list is just a sequence of strings. There is no way for the first string to be undefined and the second to be defined, but if you have control over the program, or the person who wrote it anticipated this scenario, perhaps it supports passing in an empty first argument, or perhaps a specific string which is interpreted as "undefined".

To pass in an empty string, the shell allows you to put two adjacent quotes (which will be removed by the shell before the argument is passed on to the program you are running, by way of how quotes are handled by the shell in general).

program '' second third fourth

A common related convention is to let a lone or double dash signify "an option which isn't an option".

program -- second third fourth

If you have control over the command and its argument handling (and it's not already cemented because you have programs written by other people which depend on the current behavior) a better design would be to make the optional argument truly optional, i.e. maybe make the first argument a dash option.

program --uppercase string of arguments
program --lowercase STRING OF SHOUTING
program The arguments will be passed through WITHOUT case conversion

The implementation is straightforward:

toupper () { tr '[:lower:]' '[:upper:]'; }
tolower () { tr '[:upper:]' '[:lower:]'; }
case $1 in
  --uppercase) shift; toupper;;
  --lowercase) shift; tolower;;
  *) cat;;
esac <<<"$@"

If the behavior is cemented, a way forward is to create a command with a different name with the same core behavior but with better command-line semantics, and eventually phase out the old version with the clumsy argument handling.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • The `<<<` here-string is a Bash-only construct; other than that, this shoud be portable to POSIX `sh`. – tripleee Sep 28 '17 at 17:17