在 bash 函数调用中找不到命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12616623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:23:39  来源:igfitidea点击:

Command not found on bash function call

bashshelldate

提问by JBoy

Hi i'm new to bash scripting but cannot understand why i get the command not founderror when i try to assign to a local variable the result of this function call with parameters 20120920 5.

嗨,我是新来的bash脚本,但不明白为什么我得到的命令没有发现当我试图给一个局部变量与参数20120920 5这个函数调用的结果误差。

    #!/bin/bash

function nDaysAgo () #date # daysago
{
 date -d " -  days" +%Y%m%d;
}

so the script name is ndaysago, i'm first invoking the script with . ndaysago and then assigning the value like this:

所以脚本名称是 ndaysago,我首先使用 . ndaysago 然后像这样分配值:

newdate= nDaysAgo 20120910 5

it prints: 20120905: command not found

它打印:20120905:找不到命令

Meaning that the date execution is made but then tries to use the output as command, thats not what i would expect.

这意味着执行日期但随后尝试将输出用作命令,这不是我所期望的。

i have also tried assigning the new value to a variable within the function like so:

我还尝试将新值分配给函数中的变量,如下所示:

#!/bin/bash

function nDaysAgo () #date # daysago
{
 var=$(date -d " -  days" +%Y%m%d)
}

but still nothing, mmmmmm

但还是什么都没有,嗯嗯

回答by dogbane

Spaces are not allowed around the =when assigning a variable. To invoke a function you should use the $(...)syntax which is called command substitution.

=分配变量时,周围不允许有空格。要调用函数,您应该使用$(...)称为命令替换的 语法。

Change to:

改成:

newdate=$(nDaysAgo 20120910 5)