Issue
I'm trying to achieve the following but in a .bat
file. It executes all arguments after the first one, after having set the path.
# Add your local node_modules bin to the path for this command
export PATH="./node_modules/.bin:$PATH"
# execute the rest of the command
exec "$@"
I've got the first part (I think) but don't know how to do the second part, and have not been successful in Googling the solution.
REM Add your local node_modules bin to the path for this command
SET PATH=.\node_modules\.bin;%PATH%
Solution
The first command line could be:
@set "PATH=%~dp0node_modules\.bin;%PATH%"
This command line adds to local PATH
environment variable at beginning the path of subdirectory .bin
in subdirectory node_modules
in directory of the batch file instead of the current directory.
%~dp0
expands always to the batch file directory path ending with a backslash. For that reason %~dp0
should be always concatenated with a folder/file name without an additional backslash as done here.
It would be possible to use %CD%\
instead of %~dp0
to add the path of subdirectory .bin
in subdirectory node_modules
in the current directory to local PATH
environment variable. But please be aware that current directory can be always different to batch file directory and is for that reason most likely not good here.
%CD%
expands to a directory path string not ending with a backslash, except the current directory is the root directory of a drive in which case %CD%
expands to drive letter + colon + backslash. Therefore the usage of %CD%
would require the command line:
@if not "%CD:~-1%" == "\" (set "PATH=%CD%\node_modules\.bin;%PATH%") else set "PATH=%CD%node_modules\.bin;%PATH%"
The second command line could be:
@%*
That very short command line results in getting interpreted all arguments passed to the batch file with exception of argument 0 as command line to execute by Windows command processor after parsing it. See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
@
at beginning of a command line results in Windows command processor cmd.exe
processing the batch file does not output the command line after parsing it. The command line with command set
and with %*
do not need anymore @
at beginning of the line with @echo off
at top of the batch file.
@echo off
set "PATH=%~dp0node_modules\.bin;%PATH%"
%*
Open a command prompt, run call /?
and read the output help explaining how batch file arguments can be referenced in a batch file.
See also SS64.com which has a reference for Windows CMD and Linux Shell commands.
Answered By - Mofi Answer Checked By - Robin (WPSolving Admin)