Skip to content

Instantly share code, notes, and snippets.

@FleXoft
Created November 7, 2018 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FleXoft/6912bf628f8431b2ebe904d0ba85592f to your computer and use it in GitHub Desktop.
Save FleXoft/6912bf628f8431b2ebe904d0ba85592f to your computer and use it in GitHub Desktop.
Bash script that handles optional input arguments
$ 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