Is there a way to prevent percent expansion of env variable in Windows command line?

可紊 提交于 2019-11-28 12:41:22

To provide an alternative to MC ND's helpful answer:

If you really need to get the shell involved (which is unlikely, because you state that you want the command to work both with Windows' cmd.exe and Bash), consider the solution immediately below; for a shell-less alternative that bypasses the problem, see the solution at the bottom.

Tip of the hat to MC ND for improving my original approach by suggesting placing double-quotes around % instances rather than the potential variable names between % instances, and for suggesting a clarification re execFileSync.


"Escaping" % chars. for cmd.exe

As stated in MC ND's answer, you cannot technically escape % at the Windows command prompt (inside batch files you can use %%, but that doesn't work when invoking shell commands from other environments such as Node.js, and generally wouldn't work across platforms).

However, the workaround is to place double-quotes around each % instance:

// Input shell command.
var shellCmd = 'git log --format="%C(cyan)%cd%Creset %s" --date=short -5'

// Place a double-quote on either end of each '%'
// This yields (not pretty, but it works):
//   git log --format=""%"C(cyan)"%"cd"%"Creset "%"s" --date=short -5
var escapedShellCmd = shellCmd.replace(/%/g, '"%"')

// Should work on both Windows and Unix-like platforms:
console.log(require('child_process').execSync(escapedShellCmd).toString())

The inserted double-quotes prevent cmd.exe from recognizing tokens such as %cd% as variable references ("%"cd"%" won't get expanded).

This works, because the extra double-quotes are ultimately stripped from the string when processed by the target program:

  • Windows: git.exe (presumably via the C runtime) then takes care of stripping the extra double-quotes from the combined string.

  • Unix-like (POSIX-like shells such as Bash): the shell itself takes care of removing the double-quotes before passing them to the target program.

    • Caveat: Using double-quotes in your command in general means that you need to watch out for POSIX-like shells performing potentially unwanted expansions on $-prefixed tokens (not an issue here); however, in order to remain Windows-compatible you must use double-quotes.

Technically, applying this technique to a double-quoted string breaks it into a sequence of double-quoted substrings interspersed with unquoted % instances. POSIX-like shells still recognize this as a single string - the substrings are double-quoted and directly abut the % instances. (If you apply the technique to an unquoted string, the logic is reversed: you're effectively splicing in double-quoted % instances.) The double-quotes around the substrings, which are considered syntactical elements rather than part of the string, are then removed when the substrings are joined together to form the single literal to pass to the target program.


Bypassing the problem by avoiding the shell altogether

Note: The following builds on execFile[Sync], which only works for calling external executables (which is true in the OP's case: git.exe) - by contrast, for calling shell builtins (internal commands) or Windows batch files, you cannot avoid exec[Sync] and thus interpretation by cmd.exe (on Windows).[1]

If you use execFileSync rather than execSync, the shell (cmd.exe on Windows) will NOT be involved and thus you needn't worry about escaping % chars. or any other shell metacharacters, for that matter:

require('child_process').execFileSync('git', 
   [ 'log', 
     '--format=%C(cyan)%cd%Creset %s',
     '--date=short',
     '-5' ], {stdio: 'inherit'})

Note how the arguments must be supplied individually as elements of an array, and without embedded quoting.


[1] On Windows, script files (e.g., Python scripts) cannot be called directly with execFile[Sync], but you can instead pass the interpreter executable (e.g., python) as the file to execute, and the script file as an argument. On Unix-like platforms you can call scripts directly, as long as they have a shebang line and are marked as executable.
execFile[Sync] can be used to invoke Windows batch files, but cmd.exe invariably interpolates the arguments, as with exec[Sync].

MC ND

In short, you can not do it or at least there is not a general method

note I was wrong. mklement0's answer pointed a way for a general solution.

Of course, you can use ^, not to escape the percent signs (you can not escape the percent signs at command line level) but to separate the percent sign from the name of the variable so it is not detected as a variable to be expanded. That is, for your case, it is enough to use

git log --format="%C(cyan)%cd^%Creset %s" --date=short -5

but as you point the caret is not consumed by the parser and is included in the output string.

So, any "escape" characters will be included in the output both in cmd and bash, but it can be solved for this particular (or similar) case using

process.env.cd = '%cd%'
require('child_process').execSync(
    'git log --format="%C(cyan)%cd%Creset %s" --date=short -5'
    , {stdio: 'inherit'}
)

Basically, what the code does is assign a fake value to the cd environment variable, changing it to the literal %cd%, so, when the cmd parser executes the variable expansion, the correct value ends in the command line.

But, maybe (I have not checked how/what the git code does) changing the cd variable could interfere. So, also for this case, instead of changing the value of the cd variable you can use

process.env['C(cyan)'] = '%C(cyan)%';

Why? When the cmd parser processes the command line, it will match the start of the format string against the existing variable, do the expansion and in the same process consume the starting percent sign in the %cd% variable, so it is not expanded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!