Created
October 11, 2017 17:58
-
-
Save cesc1989/506fac067dcc085b7023ce2e59b44e75 to your computer and use it in GitHub Desktop.
Different ways for declaring a function's arguments in Bash scripts
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
#!/bin/bash -xe | |
function byPosition () { | |
echo "Variable in position 1: $1" | |
echo "Variable in position 2: $2" | |
} | |
byPosition hola mundo |
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
#!/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 |
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resources:
getopts