I issued the command: git reset HEAD@{2} and got an error: unknown switch `e'.
What's happening?
CodePudding user response:
In PowerShell, unlike in cmd.exe
, @
and {
/ }
are metacharacters that either require individual escaping with `
(the so-called backtick) or enclosing the entire argument in quotes:
Therefore:
# Metacharacter-individual escaping
git reset HEAD`@`{2`}
# Enclosing the whole argument in quotes
git reset 'HEAD@{2}'
Note:
Strictly speaking ,
@
is only a metacharacter at the start of an argument.The above uses a verbatim (single-quoted) string (
'...'
); if string expansion (interpolation) is needed, use an expandable (double-quoted) string ("..."
)For a list of all of PowerShell's metacharacters in argument[-parsing] mode, see this answer.
As for the obscure error message you saw:
Unescaped use of
{...}
causes the latter to be interpreted as a script block.Due to a bug, still present as of PowerShell 7.3.1, the presence of a script block inappropriately triggers translation of such an argument to a Base64-encoded argument passed to an
-encodedCommand
parameter, which is only meaningful when calling the PowerShell CLI (powershell.exe
for Windows PowerShell,pwsh
for PowerShell (Core) 7 ) - see GitHub issue #10842.