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

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"

#!/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=

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.