3

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.

Bala
  • 3,938
  • 3
  • 19
  • 17
  • 1
    Why do you want to put the command in a variable? Why not just execute the command directly? – Dennis Williamson Feb 10 '11 at 00:28
  • @Dennis - The patch file's home dir is detected at runtime. Another script detects the home dir as "/home/my dir/" and passes it as an argument to this script. This script appends "vtk.patch" to the detected home dir and does an svn up on the patch file. – Bala Feb 10 '11 at 00:32
  • 1
    Please see ["I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050 "BashFAQ/050"). – Dennis Williamson Feb 10 '11 at 00:38
  • 1
    @Bata that doesn't answer the question, why not do `svn up "$patch_file"` instead of putting the whole command in the `$cmd` variable, which is redundant. – ocodo Feb 10 '11 at 00:39
  • 1
    You can build up the directory in a variable and pass that as an argument to the executable without also putting that (or other arguments) in the variable. `svn up "$dir_and_filename_var"`. – Dennis Williamson Feb 10 '11 at 00:40
  • 1
    Please also see ["How do I determine the location of my script?"](http://mywiki.wooledge.org/BashFAQ/028 "BashFAQ/028"). – Dennis Williamson Feb 10 '11 at 00:44

2 Answers2

4

Use eval $cmd, instead of plain $cmd

Kevin Beck
  • 2,394
  • 2
  • 15
  • 27
  • 3
    @Bala: Be aware of the [security risks of `eval`](http://mywiki.wooledge.org/BashFAQ/048 "BashFAQ/048"). – Dennis Williamson Feb 10 '11 at 00:45
  • Why tell someone to `eval $cmd` (and take all the security risks associated with `eval`) when they could just stop assigning their command to a variable in the first place? – Charles Duffy Jan 20 '21 at 01:31
  • [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) describes how to avoid needing to assign a command to a string variable for all the common cases that would lead someone to do such a thing. – Charles Duffy Jan 20 '21 at 01:32
0

Did you try escaping the space?
As a rule UNIX shells don't like non-standard characters in file names or folder names. The normal way of handling this is to escape the offending character. Try:

patch_file="/home/my\ dir/vtk.patch"

Note the backslash.

Dave
  • 1,234
  • 13
  • 24
  • 2
    Yep, I tried it. Outputs, Skipped '"/home/my\' Skipped 'dir/vtk.patch"' – Bala Feb 10 '11 at 00:33
  • @Dave, this isn't an escaping problem: syntax characters, including quotes and escapes, aren't parsed after parameter expansion is complete. Read [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) describing the OP's issue and the range of available best-practice solutions (none of which involve `eval`!) – Charles Duffy Jan 20 '21 at 01:33