Bash: weird parse output? [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
-1
down vote
favorite
This question already has an answer here:
Which are (bash) shell special parameters?
2 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
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?
bash shell-script shell command-line terminal
marked as duplicate by jasonwryan, Isaac, G-Man, RalfFriedl, roaima Dec 4 at 8:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 1 more comment
up vote
-1
down vote
favorite
This question already has an answer here:
Which are (bash) shell special parameters?
2 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
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?
bash shell-script shell command-line terminal
marked as duplicate by jasonwryan, Isaac, G-Man, RalfFriedl, roaima Dec 4 at 8:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Your quoting is broken: that is Process ID (PID) of the script itself.
– jasonwryan
Dec 3 at 22:02
sorry, where is it broken exactly?
– user324071
Dec 3 at 22:04
"expands the variable$$: you want single quotes:'.
– jasonwryan
Dec 3 at 22:04
oooh, i see. so this cant be resolved in the script to make it possible to use"$$$$"as an argument?
– user324071
Dec 3 at 22:06
1
The expansion happens before it is passed to your script. You have to use single quotes'$$$$'if you want literal$characters passed to your script.
– jordanm
Dec 3 at 22:11
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
This question already has an answer here:
Which are (bash) shell special parameters?
2 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
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?
bash shell-script shell command-line terminal
This question already has an answer here:
Which are (bash) shell special parameters?
2 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
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?
This question already has an answer here:
Which are (bash) shell special parameters?
2 answers
Why does my shell script choke on whitespace or other special characters?
4 answers
bash shell-script shell command-line terminal
bash shell-script shell command-line terminal
edited Dec 3 at 22:05
jasonwryan
48.9k14134184
48.9k14134184
asked Dec 3 at 21:58
user324071
11
11
marked as duplicate by jasonwryan, Isaac, G-Man, RalfFriedl, roaima Dec 4 at 8:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jasonwryan, Isaac, G-Man, RalfFriedl, roaima Dec 4 at 8:51
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Your quoting is broken: that is Process ID (PID) of the script itself.
– jasonwryan
Dec 3 at 22:02
sorry, where is it broken exactly?
– user324071
Dec 3 at 22:04
"expands the variable$$: you want single quotes:'.
– jasonwryan
Dec 3 at 22:04
oooh, i see. so this cant be resolved in the script to make it possible to use"$$$$"as an argument?
– user324071
Dec 3 at 22:06
1
The expansion happens before it is passed to your script. You have to use single quotes'$$$$'if you want literal$characters passed to your script.
– jordanm
Dec 3 at 22:11
|
show 1 more comment
1
Your quoting is broken: that is Process ID (PID) of the script itself.
– jasonwryan
Dec 3 at 22:02
sorry, where is it broken exactly?
– user324071
Dec 3 at 22:04
"expands the variable$$: you want single quotes:'.
– jasonwryan
Dec 3 at 22:04
oooh, i see. so this cant be resolved in the script to make it possible to use"$$$$"as an argument?
– user324071
Dec 3 at 22:06
1
The expansion happens before it is passed to your script. You have to use single quotes'$$$$'if you want literal$characters passed to your script.
– jordanm
Dec 3 at 22:11
1
1
Your quoting is broken: that is Process ID (PID) of the script itself.
– jasonwryan
Dec 3 at 22:02
Your quoting is broken: that is Process ID (PID) of the script itself.
– jasonwryan
Dec 3 at 22:02
sorry, where is it broken exactly?
– user324071
Dec 3 at 22:04
sorry, where is it broken exactly?
– user324071
Dec 3 at 22:04
" expands the variable $$: you want single quotes: '.– jasonwryan
Dec 3 at 22:04
" expands the variable $$: you want single quotes: '.– jasonwryan
Dec 3 at 22:04
oooh, i see. so this cant be resolved in the script to make it possible to use
"$$$$" as an argument?– user324071
Dec 3 at 22:06
oooh, i see. so this cant be resolved in the script to make it possible to use
"$$$$" as an argument?– user324071
Dec 3 at 22:06
1
1
The expansion happens before it is passed to your script. You have to use single quotes
'$$$$' if you want literal $ characters passed to your script.– jordanm
Dec 3 at 22:11
The expansion happens before it is passed to your script. You have to use single quotes
'$$$$' if you want literal $ characters passed to your script.– jordanm
Dec 3 at 22:11
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
$$ is a special variable that expands to the process id of the shell, in this case on the command line before your script even starts. Try something like echo $$. You'll need to escape the dollar signs with backslashes or put them in single quotes to not have them expand, i.e. echo '$$$$'.
The double quotes don't help, they'll just prevent word splitting of the expanded value, just like with regular variables ($foo vs. "$foo").
Also, in your loop, you shift only once even in the cases where you use the second argument too. This means that the arguments to your options are also processed as options themselves:
$ ./test.sh --one --two --three --four --five xyz
--two
--three
--four
--five
xyz
If you don't want that, you'll need to shift an additional time after using $2:
while [[ "$#" != 0 ]] ; do
case "$1" in
--one)
varone="$2"
shift
;;
--two)
vartwo="$2"
shift
;;
# ...
esac
shift
done
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
$$ is a special variable that expands to the process id of the shell, in this case on the command line before your script even starts. Try something like echo $$. You'll need to escape the dollar signs with backslashes or put them in single quotes to not have them expand, i.e. echo '$$$$'.
The double quotes don't help, they'll just prevent word splitting of the expanded value, just like with regular variables ($foo vs. "$foo").
Also, in your loop, you shift only once even in the cases where you use the second argument too. This means that the arguments to your options are also processed as options themselves:
$ ./test.sh --one --two --three --four --five xyz
--two
--three
--four
--five
xyz
If you don't want that, you'll need to shift an additional time after using $2:
while [[ "$#" != 0 ]] ; do
case "$1" in
--one)
varone="$2"
shift
;;
--two)
vartwo="$2"
shift
;;
# ...
esac
shift
done
add a comment |
up vote
0
down vote
$$ is a special variable that expands to the process id of the shell, in this case on the command line before your script even starts. Try something like echo $$. You'll need to escape the dollar signs with backslashes or put them in single quotes to not have them expand, i.e. echo '$$$$'.
The double quotes don't help, they'll just prevent word splitting of the expanded value, just like with regular variables ($foo vs. "$foo").
Also, in your loop, you shift only once even in the cases where you use the second argument too. This means that the arguments to your options are also processed as options themselves:
$ ./test.sh --one --two --three --four --five xyz
--two
--three
--four
--five
xyz
If you don't want that, you'll need to shift an additional time after using $2:
while [[ "$#" != 0 ]] ; do
case "$1" in
--one)
varone="$2"
shift
;;
--two)
vartwo="$2"
shift
;;
# ...
esac
shift
done
add a comment |
up vote
0
down vote
up vote
0
down vote
$$ is a special variable that expands to the process id of the shell, in this case on the command line before your script even starts. Try something like echo $$. You'll need to escape the dollar signs with backslashes or put them in single quotes to not have them expand, i.e. echo '$$$$'.
The double quotes don't help, they'll just prevent word splitting of the expanded value, just like with regular variables ($foo vs. "$foo").
Also, in your loop, you shift only once even in the cases where you use the second argument too. This means that the arguments to your options are also processed as options themselves:
$ ./test.sh --one --two --three --four --five xyz
--two
--three
--four
--five
xyz
If you don't want that, you'll need to shift an additional time after using $2:
while [[ "$#" != 0 ]] ; do
case "$1" in
--one)
varone="$2"
shift
;;
--two)
vartwo="$2"
shift
;;
# ...
esac
shift
done
$$ is a special variable that expands to the process id of the shell, in this case on the command line before your script even starts. Try something like echo $$. You'll need to escape the dollar signs with backslashes or put them in single quotes to not have them expand, i.e. echo '$$$$'.
The double quotes don't help, they'll just prevent word splitting of the expanded value, just like with regular variables ($foo vs. "$foo").
Also, in your loop, you shift only once even in the cases where you use the second argument too. This means that the arguments to your options are also processed as options themselves:
$ ./test.sh --one --two --three --four --five xyz
--two
--three
--four
--five
xyz
If you don't want that, you'll need to shift an additional time after using $2:
while [[ "$#" != 0 ]] ; do
case "$1" in
--one)
varone="$2"
shift
;;
--two)
vartwo="$2"
shift
;;
# ...
esac
shift
done
answered Dec 4 at 0:31
ilkkachu
54.4k782148
54.4k782148
add a comment |
add a comment |
1
Your quoting is broken: that is Process ID (PID) of the script itself.
– jasonwryan
Dec 3 at 22:02
sorry, where is it broken exactly?
– user324071
Dec 3 at 22:04
"expands the variable$$: you want single quotes:'.– jasonwryan
Dec 3 at 22:04
oooh, i see. so this cant be resolved in the script to make it possible to use
"$$$$"as an argument?– user324071
Dec 3 at 22:06
1
The expansion happens before it is passed to your script. You have to use single quotes
'$$$$'if you want literal$characters passed to your script.– jordanm
Dec 3 at 22:11