Equivalent of Bash's $() on Windows?
On Bash, I can use a subshell to fill the result of one command into the next:
$ echo $(sub-command args)
and if $(sub-command args)
wrote foo
to standard out, then the above command would be equivalent to:
$ echo foo
Does Windows 7 have a way to do this in cmd.exe
?
The reason I want to do this is so I can easily run executables from npm's installation directory without having that directory on my path:
$ $(npm bin)/grunt build
as they already stated in the comments, there is no native way of doing this with cmd. However there is a hack that you may use for your case (as npm bin
only returns one line):
for /F "tokens=*" %n IN ('npm bin') DO @(%n\grunt build)
or inside a batch file you have to duplicate the %
:
for /F "tokens=*" %%n IN ('npm bin') DO @(%%n\grunt build)
Alternatives
Batch
You could generate a script that receives the parameter and runs the command, something like
:: Takes the parameters and send to npm bin output
for /f "tokens=*" %%n in ('npm bin') do @(%%n\%1 %2 %3 %4 %5)
Save it as npmbin.cmd in your path, then you could run:
npmbin.cmd grunt build
Powershell
For your case, you should be able to invoke it through powershell using the invoke-expression cmdlet:
iex "$(npm bin)\grunt build"
I don't have npm on me right now, but I tested with other program and it looks like it would work.
Environment Variable
Anyway, I think the previous are very complicated "solutions", while you only want to simplify your command. I was wondering if you could create a global variable, say NPMBIN
and then use it for your commands:
%NPMBIN%\grunt build
To extend this you could set up a task to set it programatically for your user (or eventually the computer) using setx
, some batch like:
for /f "tokens=*" %%n in ('npm bin') do @(setx NPMBIN %%n)
Hope it helps. Regards