如何在Powershell中执行与$ PROGPATH/程序相同的bash? [英] How do I do the bash equivalent of $PROGPATH/program in Powershell?

查看:66
本文介绍了如何在Powershell中执行与$ PROGPATH/程序相同的bash?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GNU/Linux中,我会这样做:

In GNU/Linux I would do:

PROGPATH=/long/and/complicated/path/to/some/bin
$PROGPATH/program args...

但是在Powershell中,如果我尝试这样做:

but in Powershell if I try this:

$PROGPATH=\long\and\complicated\path\to\some\bin
$PROGPATH\program args...

我得到:

At script.ps1:2 char:...
+ $PROGPATH\program args ...
+          ~~~~~~~~
Unexpected token '\program' in expression or statement.
+ CategoryInfo          : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

那么我该如何在Powershell中的bash中执行此简单的操作呢?

So how do I do this simple thing I know how to do in bash, in Powershell?

推荐答案

js2010的有用答案显示了正确的解决方案:

js2010's helpful answer shows the correct solution:

由于您的命令名称/路径包含变量引用( $ PROGPATH/...),因此必须使用& .
如果分组表达式,使用(...)

Because your command name/path contains a variable reference ($PROGPATH/...), you must invoke it with &.
The same applies if a grouping expression, (...) is used, or a subexpression, $(...) is involved.

此外,如果引用('...'"......") [1] ,例如,如果路径包含 space ,则为必填项.

Additionally, the same applies if a command name/path is quoted ('...' or "...")[1], as is required if the path contains spaces, for instance.

换句话说:仅当命令名称/路径是 verbatim,不带引号的字符串 [1] 时,才支持直接调用;在所有其他情况下,必须使用& .

To put it differently: Direct invocation is only supported if the command name/path is a verbatim, unquoted string[1]; in all other cases, & must be used.

关于为什么:

& 调用运算符 对于将语句强制解释为 command 是必需的,即使其以 argument模式(请参见下文)进行解析,以便导致执行命令而不是表达式评估

&, the call operator is necessary to force interpretation of a statement as a command, i.e. to have it parsed in argument mode (see below), so as to result in command execution rather than expression evaluation.

PowerShell具有两个基本的解析模式 :

PowerShell has two fundamental parsing modes:

  • 参数模式 ,它的作用类似于传统外壳 ,其中第一个标记是 命令名称/路径 (例如cmdlet或外部程序),其后的标记代表参数,仅当它们包含外壳元字符(对于PowerShell具有特殊含义的字符,例如用于分隔令牌的空格).

  • argument mode, which works like a traditional shell, where the first token is a command name/path, such as a cmdlet or an external program, with subsequent tokens representing the arguments, which only require quoting if they contain shell metacharacters (chars. with special meaning to PowerShell, such as spaces to separate tokens).

表达模式 ,它类似于使用编程语言 的表达式.

expression mode, which works like expressions in programming languages.

PowerShell根据语句的第一个标记决定要应用哪种解析模式:

PowerShell decides based on a statement's first token what parsing mode to apply:

除其他事项外,如果第一个令牌以变量引用开头或为带引号的字符串,则PowerShell将以表达式模式进行解析 .

If, among other things, the first token starts with a variable reference or is a quoted string, PowerShell parses in expression mode.

  • 在表达式模式下, \ 启动新令牌,而无法识别的令牌 \ program 会导致您看到语法错误.
  • (如果使用过/,它将被解释为除法运算符,而 program 将不是有效的除数操作数.)
  • In expression mode, \ starts a new token, and unrecognized token \program results in the syntax error you saw.
  • (If you had used /, it would have been interpreted as the division operator, and program wouldn't be a valid divisor operand.)

[1]请注意,如果可执行文件路径是 literal 字符串(不包含表达式的变量引用),则可以选择` -escape 单个字符(空格),而不是将整个字符串括在'...'"..." 中,在这种情况下<然后就不需要code>& ;例如:
C:\ Program` Files \ Notepad ++ \ notepad ++.exe
使用文字字符串,您甚至可以使用 partial 单引号或双引号,只要 first 标记是 unquoted 即可;例如:
C:\程序文件" \ Notepad ++ \ notepad ++.exe

[1] Note that if your executable path is a literal string (doesn't contain variable references of expressions) you may alternatively `-escape individual characters (spaces) in lieu of enclosing entire string in '...' or "...", in which case & is then not necessary; e.g.:
C:\Program` Files\Notepad++\notepad++.exe
With a literal string you can even employ partial single- or double-quoting as long as the first token is unquoted; e.g.:
C:\"Program Files"\Notepad++\notepad++.exe

这篇关于如何在Powershell中执行与$ PROGPATH/程序相同的bash?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆