Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can you perform command substitutions at the Windows command-prompt?

Command substitution is a very powerful concept of the UNIX shell. It is used to insert the output of one command into a second command. E.g. with an assignment:

$ today=$(date) # starts the "date" command, captures its output
$ echo "$today"
Mon Jul 26 13:16:02 MEST 2004

This can also be used with other commands besides assignments:

$ echo "Today is $(date +%A), it's $(date +%H:%M)"
Today is Monday, it's 13:21

This calls the date command two times, the first time to print the week-day, the second time for the current time.

I need to know to do that in the command-prompt, (I already know that there is a way to perform something like that using as part of the for command, but this way is much more obfuscated and convoluted.

share|improve this question
1  
Do you actually need to do this in MS-DOS?! (Note: Windows is NOT MS-DOS. The Windows command line is NOT MS-DOS.) Now that I've gotten that out of the way, if you actually are on Windows you can use PowerShell (comes with Vista or later, available as download from Microsoft for XP). That does have command substitution. Or use one of the POSIX shells, as suggested in an answer. –  Bob Oct 31 '12 at 16:47
add comment

2 Answers

up vote 2 down vote accepted

You cannot do that in DOS.

If by DOS you mean the Windows Command Processor cmd.exe then you can get the output of a command with for /f:

for /f %%x in ('date') do set "today=%%x"

Depending on your requirements this can get a little more complex.

share|improve this answer
    
yes i do, but thats the problem i have. because it's much more complex to use it in nested commands or in large sentences, in fact that's the way i already knew to do it; but I would like to know a simplest form, especially for nested commands. –  wiggin200 Oct 31 '12 at 14:06
    
This is its simplest form. –  Joey Oct 31 '12 at 14:08
add comment

If you'd like a genuine Unix-style command substitution, not the more limited facility you get with cmd.exe, consider getting a genuine Unix shell for Windows. Two that I can suggest are Cygwin (free, but doesn't understand the Windows filesystem very well) and my own Hamilton C shell (commercial, but written for Windows).

share|improve this answer
add comment

Your Answer

 
discard

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.