Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a batch script to automate build events on Windows, and as part of it I need to change some lines in a few files. So I'm going to use sed. But how do I use variables in sed inside a batch script?

share|improve this question
How are you going to use a unix utility on Windows? What version of Windows are you using. We need more specifics. – Ramhound Mar 4 '13 at 18:03
That would be another question. I'm using Win7, but I've been told I have to use sed for the modification of the files. – SSumner Mar 4 '13 at 18:04
And there are versions of sed for Windows: – SSumner Mar 4 '13 at 18:09
Do you have to use sed, No. Might it be easier to use sed, Yes. Just want to make sure you understand that you are not limited to just sed. See… – David Ruhmann Mar 4 '13 at 21:58
I'm told I have to use sed. I'm aware there are other ways to solve the problem, but those are the constraints I have been given – SSumner Mar 4 '13 at 23:35
up vote 3 down vote accepted

Refer to the variables using the usual %varname% syntax used by cmd.exe. As shown in this example, I've defined two variables old and new and substituted them into a sed expression on the command line. Typing original into the input, sed echos back new and improved.

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

c:\Users\Nicole>set old=original

c:\Users\Nicole>set new=new and improved

c:\Users\Nicole>sed "s/%old%/%new%/"
new and improved
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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