Skip to content

Instantly share code, notes, and snippets.

@inhumantsar
Created October 22, 2014 19:54
Show Gist options
  • Save inhumantsar/3248f95b5165b6abd632 to your computer and use it in GitHub Desktop.
Save inhumantsar/3248f95b5165b6abd632 to your computer and use it in GitHub Desktop.
Bash Snippets

From: http://stackoverflow.com/questions/16186392/multiple-option-arguments-using-getopts-bash

#!/bin/bash

while getopts "m:" opt; do
    case $opt in
        m) multi+=("$OPTARG");;
        #...
    esac
done
shift $((OPTIND -1))

echo "The first value of the array 'multi' is '$multi'"
echo "The whole list of values is '${multi[@]}'"

echo "Or:"

for val in "${multi[@]}"; do
    echo " - $val"
done

The output would be:

$ /tmp/t
The first value of the array 'multi' is ''
The whole list of values is ''
Or:

$ /tmp/t -m "one arg with spaces"
The first value of the array 'multi' is 'one arg with spaces'
The whole list of values is 'one arg with spaces'
Or:
 - one arg with spaces

$ /tmp/t -m one -m "second argument" -m three
The first value of the array 'multi' is 'one'
The whole list of values is 'one second argument three'
Or:
 - one
 - second argument
 - three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment