bash中$(3-1)的输出
我发现有一件事我无法在bash中解释 命令:bash中$(3-1)的输出,bash,Bash,我发现有一件事我无法在bash中解释 命令: $(3-1) 3-1: command not found 如果我一步一步地做,它将显示如下: 3-1 3-1: command not found 3-1: command not found 3-1:: command not found 有两个冒号。为什么会有差异?您输入: foo 输出: foo: command not found 您可以输入: foo bar 输出: foo: command not found foo::
$(3-1)
3-1: command not found
如果我一步一步地做,它将显示如下:
3-1
3-1: command not found
3-1: command not found
3-1:: command not found
有两个冒号。为什么会有差异?您输入:
foo
输出:
foo: command not found
您可以输入:
foo bar
输出:
foo: command not found
foo:: command not found
因此bash认为空间分隔命令/参数。如果使用:
发出的命令,您也会在输入中得到它:
输入:
foo:
输出:
foo: command not found
foo:: command not found
将foo
替换为3-1
,您可以找到答案。您可以输入:
foo
输出:
foo: command not found
您可以输入:
foo bar
输出:
foo: command not found
foo:: command not found
因此bash认为空间分隔命令/参数。如果使用:
发出的命令,您也会在输入中得到它:
输入:
foo:
输出:
foo: command not found
foo:: command not found
将
foo
替换为3-1
,您将找到答案。当您键入bash
无法找到的任何命令时,您将收到表单的错误消息
bash: <cmd>: command not found
如果您键入3-1:
,您将得到
$ 3-1
bash: 3-1: command not found
$ 3-1:
bash: 3-1:: command not found
当您键入
bash
找不到的任何命令时,您会收到以下形式的错误消息
bash: <cmd>: command not found
如果您键入3-1:
,您将得到
$ 3-1
bash: 3-1: command not found
$ 3-1:
bash: 3-1:: command not found
您将如何处理
3-1
?如果您想执行它,我怀疑您是否有程序3-1
或2
。试着这样做:
n=$((3-1))
echo $((3-1))
我认为这里需要解释:
如果你用bash写新行
do_smth
$(do_smth)
它假设您想要执行一个程序。$(do_smth)
的意思是:执行一个程序do_smth
,并捕获输出。所以,如果您在bash新行中编写
do_smth
$(do_smth)
它假定您要执行
do_smth
,从命令中获取一个输出,并使用它的名称执行命令<代码>$($do_smth))是另一个。它假定$do\smth
是一个数学表达式,并允许您计算类似c的数学表达式。您将如何处理3-1
?如果您想执行它,我怀疑您是否有程序3-1
或2
。试着这样做:
n=$((3-1))
echo $((3-1))
我认为这里需要解释:
如果你用bash写新行
do_smth
$(do_smth)
它假设您想要执行一个程序。$(do_smth)
的意思是:执行一个程序do_smth
,并捕获输出。所以,如果您在bash新行中编写
do_smth
$(do_smth)
它假定您要执行
do_smth
,从命令中获取一个输出,并使用它的名称执行命令<代码>$($do_smth))是另一个。它假定$do_smth
是一个数学表达式,并允许您计算类c的数学表达式。3-1
。您收到的错误消息将发送到标准错误,而不是标准输出。因此,$(3-1)
没有运行
3-1: command not found
它正在运行
3-1
并失败。如果您试图运行错误消息,则第一个单词将作为命令,其余的是参数。也没有找到命令3-1:
,因此没有找到带有两个冒号的错误消息。3-1
。您收到的错误消息将发送到标准错误,而不是标准输出。因此,$(3-1)
没有运行
3-1: command not found
它正在运行3-1
并失败。如果您试图运行错误消息,则第一个单词将作为命令,其余的是参数。也找不到命令3-1:
,因此错误消息有两个冒号。您的“逐步”细分是错误的,因为您假定第一条“未找到命令”错误消息将成为$()
的结果,因此将作为命令重新解析。这不会发生,因为$()
仅捕获标准输出(fd 1),并且“未找到命令”消息打印到标准错误(fd 2)
如果你跑
$(3-1 2>&1)
你可能会得到更像你期望的东西。(我无法准确再现您的结果,因为我的bash错误消息与您的不太一样。)您的“逐步”细分是错误的,因为您假设第一条“未找到命令”错误消息将成为$()
的结果,因此将作为命令重新解析。这不会发生,因为$()
仅捕获标准输出(fd 1),并且“未找到命令”消息打印到标准错误(fd 2)
如果你跑
$(3-1 2>&1)
你可能会得到更像你期望的东西。(我无法准确复制您的结果,因为我的bash错误消息与您的不太一样。)shell看到:
情景1
stdin- "$(3-1)" ( run non existent command "3-1" in a subshell ... )
Command- "3-1"
Arguments-
error- 3-1
reply- "3-1" ": command not found"
情景2
stdin- "3-1:" "command" "not" "found"
Command- "3-1:"
(note that the ":" you typed is seen as part of the command)
Arguments- "command" "not" "found"
error- 3-1:
reply- "3-1:" ": command not found"
(^---^ and this is where the double colon comes from)
正如您所看到的,shell根本没有用双冒号回复强>
它与您的“3-1:”相呼应,并添加了:reply“shell看到了这一点:
情景1
stdin- "$(3-1)" ( run non existent command "3-1" in a subshell ... )
Command- "3-1"
Arguments-
error- 3-1
reply- "3-1" ": command not found"
情景2
stdin- "3-1:" "command" "not" "found"
Command- "3-1:"
(note that the ":" you typed is seen as part of the command)
Arguments- "command" "not" "found"
error- 3-1:
reply- "3-1:" ": command not found"
(^---^ and this is where the double colon comes from)
正如您所看到的,shell根本没有用双冒号回复强>
它与您的“3-1:”相呼应,并添加了:reply“不要忘记NFS路径:
server:/path/to/executable
…我只得到了-bash:3-1:command not found
不要忘记NFS路径:server:/path/to/executable
…我只得到了-bash:3-1:command not found
我的问题更多的是关于$()命令替换。为什么$(3-1)不显示双冒号?@windchime$(3-1)
与$(foo)
相同。在(..)
中没有:
,因此bash不会输出“double”:
。它的输出将与您输入3-1
然后输入相同。这就是我感到困惑的地方。我认为来自foo的任何输出都将在当前shell中用作文本命令。所以,“foo:command not found”将作为当前命令行上的命令运行。是吗?@windchime是的,但是在foo:
和未找到命令之间有一个空格,bash不认为整行都是一个命令。试试fools
我的问题更多地是关于$()命令替换的。为什么$(3-1)不显示双冒号?@windchime$(3-1)
与$(foo)
相同。您在(…)
中没有:
,因此bash不会输出“dou”