I am trying to run a windows batch file from a ssh login. I have successfully installed cygwin w/ OpenSSH on the server (windows 2003).

I can log in and run the file, but the file does not run properly because it relies on an environment variable. I discovered that for security reasons not all environment variables are added to the ssh shell. I added the environment variable I need (in ~/.profile), but the file still errors out.
I modified the file and had it output the environment variable that was causing issues before, and I discovered that it was still not set. So, even though I added the environment variable, and it exists in the shell, it is not available in the shell the batch file is being run in.

How would I add the environment variable to the batch file's shell?
Modifying the file permanently is not an option; it is created by another program, and I am trying to write a git hook to run the file on another machine after a "git push"


You could write a small shell script that sets the environment variable(s) and then runs the batch file. For example:

export VAR1="value of VAR1"
export VAR2="value of VAR2"

This example assumes that script.bat is in the current working directory and that you have execute permission for it.

  • that's what I ended up doing. the problem is that the script is in a folder that the build process of the program recreates, so it would get overwritten. It has to be called from that directory too, as it relies on the pwd variable. I worked around the issue though by creating the file on the fly as I needed it with echo commands. – AsherMaximum Jun 11 '12 at 11:21
  • You could put the above shell script in a place where it can persist, then put a cd ... command just before the ./script.bat to change to the appropriate directory just before launching the batch file. That way the above shell script doesn't have to be overwritten or deleted. – Fran Jun 11 '12 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.