1

I am following this post:

http://blog.dotsmart.net/2011/01/27/executing-cygwin-bash-scripts-on-windows/

I need to execute a batch file with an argument and pass this argument to a bash script. The argument is a long file name with spaces, something like FILE WITH SPACES.xlsx.

In Windows:

c> program.cmd "FILE WITH SPACES.xlsx" # Windows adds the quotes to complete the name of the file.

The batch script must pass the name of the file "FILE WITH SPACES.xlsx" to a .sh script, something like this:

# program.sh "FILE WITH SPACES.xlsx"

Here is the problem, the file is passed without the quotes:

# program.sh FILE WITH SPACES.xlsx

The script program.sh has:

#!/bin/sh
anyprogram "$1"

The quotes are not passed and the program anyprogram doesn't locate the file FILE WITH SPACES.xlsx.

i tried escaping the " with \ but not work:

#!/bin/sh
anyprogram "\"$1"\"

Also, with:

#!/bin/sh
anyprogram '"$1"'

None looks to work, any suggestion?

9
  • Hi. Thanks for your answer. The args are not inside the batch file, those args are dynamic. Commented Sep 16, 2015 at 3:33
  • Within your .sh script you could try in=\"$*\" then echo $in. Commented Sep 16, 2015 at 3:37
  • Can you include in your question the line in the batch file where the call to the .sh file is being done? Commented Sep 16, 2015 at 9:13
  • Replace $i by $* or (when the call is correct) $1. Commented Sep 16, 2015 at 9:28
  • @MCND I used the same code of blog post: blog.dotsmart.net/2011/01/27/… Commented Sep 16, 2015 at 16:40

1 Answer 1

2

Batch files can be tricky with the way they handle double-quotes, and adding Bash variables can make things even trickier. Here is a solution that I believe solves your specific issue:

Batch file location: C:\Users\Jamey\Desktop\blah.bat

Batch file contents:

@ECHO OFF
ECHO I AM A BATCH FILE.
CD C:\Users\Jamey\Desktop
ECHO TESTING > %1
TYPE %1
ECHO.
SET scriptpath=/cygdrive/c/users/jamey/desktop
C:\cygwin64\bin\bash.exe -c "$scriptpath/program.sh "%1""

Bash script location: C:\Users\Jamey\Desktop\program.sh

Bash script contents:

#!/bin/bash

echo "I AM A BASH SCRIPT."
echo "A WINDOWS BATCH FILE TOLD ME TO SAY THIS: $1"
echo
ls -l "$1"
echo
cat "$1"
echo
rm -v "$1"

Here is the cmd.exe output from running blah.bat "FILE NAME WITH SPACES.txt":

C:\Users\Jamey\Desktop>blah.bat "FILE NAME WITH SPACES.txt"
I AM A BATCH FILE.
TESTING

I AM A BASH SCRIPT.
A WINDOWS BATCH FILE TOLD ME TO SAY THIS: FILE NAME WITH SPACES.txt

-rwxrwx---+ 1 Jamey None 10 Sep 24 09:05 FILE NAME WITH SPACES.txt

TESTING

removed 'FILE NAME WITH SPACES.txt'

Alternately, if you replaced 'rm -v' with 'cygstart', "FILE NAME WITH SPACES.txt" would open in Notepad. Same goes for Excel with .xlsx extensions. I hope this answers your question!

Sign up to request clarification or add additional context in comments.

2 Comments

I don't know why this question or my comment got downvoted. From my perspective, I answered the question pretty accurately.
I've upvoted -- your response is correct (and helped me to solve an identical problem). One note: it would be prudent to use single-quotes to surround the script parameter instead of doubles in order to avoid bash messing with backslashes etc. (I pass a DOS path+filename from my batch file to my script then convert to unix format within the bash script using cygpath.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.