How to accommodate spaces in a variable in a bash shell script?

Bala picture Bala · Feb 10, 2011 · Viewed 7.7k times · Source

Hopefully this should be a simple one... Here is my test.sh file:

#!/bin/bash
patch_file="/home/my dir/vtk.patch"
cmd="svn up \"$patch_file\""
$cmd

Note the space in "my dir". When I execute it,

$ ./test.sh 
Skipped '"/home/my'
Skipped 'dir/vtk.patch"'

I have no idea how to accommodate the space in the variable and still execute the command. But executing this the following on the bash shell works without problem.

$ svn up "/home/my dir/vtk.patch"   #WORKS!!!

Any suggestions will be greatly appreciated! I am using the bash from cygwin on windows.

Answer

Kevin Beck picture Kevin Beck · Feb 10, 2011

Use eval $cmd, instead of plain $cmd