-
-
Save FleXoft/6912bf628f8431b2ebe904d0ba85592f to your computer and use it in GitHub Desktop.
Bash script that handles optional input arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat somecommand.sh | |
#! /usr/bin/bash | |
ARG1=${1:-foo} | |
ARG2=${2:-bar} | |
ARG3=${3:-1} | |
ARG4=${4:-$(date)} | |
echo "$ARG1" | |
echo "$ARG2" | |
echo "$ARG3" | |
echo "$ARG4" | |
Here are some examples of how this works: | |
$ ./somecommand.sh | |
foo | |
bar | |
1 | |
Thu Mar 29 10:03:20 ADT 2018 | |
$ ./somecommand.sh ez | |
ez | |
bar | |
1 | |
Thu Mar 29 10:03:40 ADT 2018 | |
$ ./somecommand.sh able was i | |
able | |
was | |
i | |
Thu Mar 29 10:03:54 ADT 2018 | |
$ ./somecommand.sh "able was i" | |
able was i | |
bar | |
1 | |
Thu Mar 29 10:04:01 ADT 2018 | |
$ ./somecommand.sh "able was i" super | |
able was i | |
super | |
1 | |
Thu Mar 29 10:04:10 ADT 2018 | |
$ ./somecommand.sh "" "super duper" | |
foo | |
super duper | |
1 | |
Thu Mar 29 10:05:04 ADT 2018 | |
$ ./somecommand.sh "" "super duper" hi you | |
foo | |
super duper | |
hi | |
you |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment