I'm working on a simple script that accepts multiple command line arguemnts in an order:
#!/bin/bash
function arg_parser () {
while [[ $# != 0 ]] ; do
case "$1" in
--one)
varone="$2"
;;
--two)
vartwo="$2"
;;
--three)
varthree="$2"
;;
--four)
varfour="$2"
;;
--five)
varfive="$2"
;;
esac
shift
done
}
arg_parser "$@"
echo $varone
echo $vartwo
echo $varthree
echo $varfour
echo $varfive
Then run it:
./test.sh --one testone --three testthree --two testtwo --five "test five" --four "$$$$"
testone
testtwo
testthree
793793
test five
Notice how --four
returns "793793" and not "$$$$"? Does anyone know why this is happening and/or how the script can be improved to prevent this from happening?
"
expands the variable$$
: you want single quotes:'
. – jasonwryan Dec 03 '18 at 22:04"$$$$"
as an argument? – user324071 Dec 03 '18 at 22:06'$$$$'
if you want literal$
characters passed to your script. – jordanm Dec 03 '18 at 22:11shift 2
because otherwise you're going through thecase
statement twice as many times as you need to. – l0b0 Dec 03 '18 at 23:36