0

my script try to combine a variable(include space) with another variable, and put them
together as a command's parameter, I put "" for the variable which includes space, but it is
always broken into two strings and make the command fail.

bash-3.1$ ./space.sh
./param -J "t space" sleep 10
Param1=-J
param2="t
param3=space"
param4=sleep
param5=10
bash-3.1$ ./param -J "t space" sleep 10
Param1=-J
param2=t space
param3=sleep
param4=10
param5=

bash-3.1$ more space.sh
#!/bin/sh
a="t space"
b="-J"
c="$b \"$a\""
res="./param $c sleep 10"
echo $res
$res

bash-3.1$ more param
#!/bin/sh

echo Param1=$1
echo param2=$2
echo param3=$3
echo param4=$4
echo param5=$5
1
Contributor
2
Replies
3
Views
9 Years
Discussion Span
Last Post by georgegao111
0

I found solution: execute the command using "sh -c command"

bash-3.1$ more space.sh
#!/bin/sh
a="t space"
b="-J"
c="$b \"$a\""
res="./param $c sleep 10"
echo $res
sh -c "$res"

0
#!/bin/sh
echo Param1=$1
echo param2=$2
echo param3=$3
echo param4=$4
echo param5=$5

bash-3.1$ ./space.sh
./param -J "t space" sleep 10
Param1=-J
param2=t space
param3=sleep
param4=10
param5=

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.