Passing string as a input parameter for a shell script

Hi

i have a shell script which needs a string as an input parameter. How to pass the string param as an input?

In command line am running the script.

for e.g.,
a="who is a buddy?"

sh sample.sh $a

Inside the script i get this input param as $1 but only the value "who" is accepted as an input. Please help me!

Thanks!

Try as..

sh sample.sh "$a"

What is the content of your sample.sh script ?

Maybe in this script you should use $* or $@ instead of $1

I tried using this, but only "who" is taken for $1

Try what MichaelRozar suggested

i tried even that. it's not working out.

# cat eu
echo $1
# a="who am i"
# sh eu $a
who
# sh eu "$a"
who am i
#

Show us what you have , what you type, what you get, and what is in your script.

1 Like

I am getting the value.. Are you using in this way..

$ cat test.sh
#!/bin/bash
echo "$1"
$ echo $a
who is a buddy?
$ sh test.sh "$a"
who is a buddy?
$
1 Like

Thanks ctsgnb and jayan_jay. Thanks the issue is that am using the $1 variable in inside a curl command thats the problem.

my curl command inside the sample.sh script is,

curl  -H "Content-type: application/json" -H "Accept: application/json" --data-binary '{"body" : "$1","newTagList" : "h"}' -H "X-JaskUid:111" -X POST "www.google.com"

in the above command, $1 value is taken as "who".

Please let me know how to pass a variable in the above curl code.

Thanks!

What does it give with "eval" ?

eval curl  -H "Content-type: application/json" -H "Accept: application/json" --data-binary '{"body" : "$1","newTagList" : "h"}' -H "X-JaskUid:111" -X POST "www.google.com"

otherwise you can give a try enclosing the "$1" into single quote :

curl  -H "Content-type: application/json" -H "Accept:  application/json" --data-binary '{"body" : '"$1"',"newTagList" : "h"}' -H  "X-JaskUid:111" -X POST "www.google.com"

tried with eval,

error thrown like,

curl: (6) Couldn't resolve host 'application'
curl: (6) Couldn't resolve host 'application'
curl: (6) Couldn't resolve host ':'
curl: (6) Couldn't resolve host 'who is he,newTagList'
curl: (6) Couldn't resolve host ':'
curl: (6) Couldn't resolve host 'h3,origTagList'
curl: (6) Couldn't resolve host ':'

if i try to give like '"1"', it throws exception.

If you need expand $1 before do curl, then $1 can't be insite ' ' and if you need " ", then

curl  -H "Content-type: application/json" -H "Accept: application/json" --data-binary '{"body" : '\"$1\"',"newTagList" : "h"}' -H "X-JaskUid:111" -X POST "www.google.com"
1 Like

Thanks it is working..!