Skip to content

Instantly share code, notes, and snippets.

@cesc1989
Created October 11, 2017 17:58
Show Gist options
  • Save cesc1989/506fac067dcc085b7023ce2e59b44e75 to your computer and use it in GitHub Desktop.
Save cesc1989/506fac067dcc085b7023ce2e59b44e75 to your computer and use it in GitHub Desktop.
Different ways for declaring a function's arguments in Bash scripts
#!/bin/bash -xe
function byPosition () {
echo "Variable in position 1: $1"
echo "Variable in position 2: $2"
}
byPosition hola mundo
#!/bin/bash -xe
function optionalArgs () {
var_1=${1:-foo}
var_2=${2:-bar}
echo "Variable 1: $var_1"
echo "Variable 2: $var_2"
}
optionalArgs uno dos
#!/bin/bash -xe
function namedWithGetOps () {
while getopts ":p:s:" opt; do
case $opt in
p) first="$OPTARG";;
s) second="$OPTARG";;
\?) echo "Invalid option -$OPTARG" >&2;;
esac
done
printf "Argument first is %s\n" "$first"
printf "Argument second is %s\n" "$second"
}
namedWithGetOps -s segundo -p primero
@cesc1989
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment