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?

6
  • 2
    Please add an example of what you would like to do and what you are trying so far stackoverflow.com/help/how-to-ask Commented Sep 27, 2017 at 2:12
  • You may need to use getopts and your answer is probably covered here stackoverflow.com/questions/192249/… Commented Sep 27, 2017 at 2:13
  • Possible duplicate of How do I parse command line arguments in Bash? Commented Sep 27, 2017 at 2: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. Commented Sep 27, 2017 at 2: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? Commented Sep 27, 2017 at 2:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The <<< here-string is a Bash-only construct; other than that, this shoud be portable to POSIX sh.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.