Bash 函数参数返回错误“找不到命令”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12238158/
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:10:28  来源:igfitidea点击:

Bash function argument returns error "command not found"

bashshell.bash-profilebash-function

提问by JKirchartz

I have this function in a bash script, to create a new jekyll post; but it returns the argument as command not found. Here's the script:

我在 bash 脚本中有这个功能,可以创建一个新的 jekyll 帖子;但它将参数作为未找到的命令返回。这是脚本:

 function new_post () {
     if [ -z "" ]
     then
         read -p "Post Title:"  TITLE
     else
         TITLE= ""
     fi
     FILE=$( echo $TITLE | tr A-Z a-z | tr ' ' _ )
     echo -e '---\nlayout: post\ntitle: '$TITLE'\npublished: false\n---\n' > $(date '+%Y-%m-%d-')"$FILE"'.md'
 }

But whenever I try to run it it returns:

但是每当我尝试运行它时,它都会返回:

$>new_post "Hello World"
-bash: Hello World: command not found

It appears to be trying to run the argument as a command.

它似乎试图将参数作为命令运行。

I even tried this and got the same result

我什至尝试了这个并得到了相同的结果

$>TITLE= "Hello World" && echo -e ---layout: post\ntitle: "$TITLE"\n--- 
-bash: Hello World: command not found

Can anybody tell me what I'm doing wrong?

谁能告诉我我做错了什么?

回答by phsym

It may be the space in TITLE= "$1"that causes the error. Try with TITLE="$1"

它可能是TITLE= "$1"导致错误的空间。试试TITLE="$1"

回答by Greg Woz

In my case:

就我而言:

echo "Deploy of `` to `` project? (Y/N)"

the issue was also present. When I removed [``] it's gone. Not sure if you pasted a complete script but beware double quotes for args.

这个问题也存在。当我删除 [``] 时,它就消失了。不确定您是否粘贴了完整的脚本,但要注意 args 的双引号。

Similar answer https://askubuntu.com/questions/180320/bash-script-program-with-parameters-as-a-single-variable-command-not-found

类似的答案https://askubuntu.com/questions/180320/bash-script-program-with-parameters-as-a-single-variable-command-not-found