3

I have a script which gets called with two parameters:

./script.sh a b

I'm currently reading them through $1 and $2 (such as cd $1). My problem is that the output needs to be the same for when the script gets called with quoted parameters:

./script.sh "a b"

which, if I understand it correctly, passes both parameters into the $1 variable. Is there a simple and clean way to do this or do I need to check the first parameter for spaces, split it into an array, pass parts of the array into two separate variables and then use them throughout the script?

4
  • Sorry its mistype: if [ $# < 2 ] ; then set -- ${1% *} ${1#* } ; fi of course.
    – Costas
    Feb 22, 2015 at 13:25
  • When I pass it to echo -e "\n$1" It still outputs "a b", whereas I need for it to be "a". Feb 22, 2015 at 13:36
  • #!/bin/bash if [ 0 < 2 ] ; then set -- ${1% *} ${1#* } ; fi echo -e "\n$1" outputs a
    – Costas
    Feb 22, 2015 at 13:45
  • Not for me, that's certainly interesting. Feb 22, 2015 at 13:50

1 Answer 1

2

The easiest thing is to put this at the top of your script:

set -- $*

This will then re-expand the parameter list, so any spaces in the parameters will become separators between new parameters.

There may be some complicated behavior if you have very convoluted quoting in your parameters (e.g., quoted quotes), but my guess is that that is not likely to happen.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .