Passing Parameter containing space in between to Shell Script

Hi,

I have one shell script which use two parameter however one of its parameter have space in between.
eg.

a.sh 20110114 b c d

here b c d is one parameter

I used 'b c d' but its not giving correct result.
Also i tried b\c\d but this one also didnt work.

Any help would be appreciated.

Thanks!!!!!!

#!/bin/sh
echo $#
echo $1
echo $2
echo $3

When I run it as:

a.sh 20110114 'b c d'

I get following:

2
20110114
b c d

Please mention what exactly you are doing.
Probably code snippet, current behavior of code and expected one.

I just want my script to accept input containing space.my first parameter is date while second one is name which contain space.

i cant write

a.sh 20110114 b c d

as it would be consider b c d as three parameter which is wrong.
So i tried to pass it as 'b c d' but it gives me wrong result even though script run.

As you can see above, In my script output,

a.sh 20110114 'b c d'

arguments count ($#) is 2
1st argument ($1) is 2011011
2nd aguument($2) is b c d

If you are passing your argument to some other command, pass it in quotes.
Unless you show your problematic code, we can't really help you to fix your issue.

Actually second argument which contain space is used to call sql procedure,so if i pass 'b c d' then it will consider input as it is including quotes.Which i don't require.

Thanks for your quick response

Execute script as:

a.sh 20110114 "b c d"

And pass 2nd argument in quotes to sql proc:

<sql_proc> "$2"

But not very sure about sql procedure. If above doesn't work, then you may do following:
remove spaces from your input(replace space with some character say underscore, or something else which should be part of real value)
Then pass your modified argument (without space) to sql procedure.
Then in sql proc, read this value and convert underscore back to space.
Like:

var=$(echo "$2" | sed "s/ /_/g")

Now pass $var to sql proc (And conver _ back to space in sql proc)

1 Like