Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question
    
Sorry its mistype: if [ $# < 2 ] ; then set -- ${1% *} ${1#* } ; fi of course. –  Costas Feb 22 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". –  LeonhardEuler Feb 22 at 13:36
    
#!/bin/bash if [ 0 < 2 ] ; then set -- ${1% *} ${1#* } ; fi echo -e "\n$1" outputs a –  Costas Feb 22 at 13:45
    
Not for me, that's certainly interesting. –  LeonhardEuler Feb 22 at 13:50

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Thank you, that's exactly what I was hoping for. –  LeonhardEuler Feb 22 at 15:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.