Bash function argument returns error “command not found”
-
29-06-2021 - |
Question
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:
function new_post () {
if [ -z "$1" ]
then
read -p "Post Title:" TITLE
else
TITLE= "$1"
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?
La solution
It may be the space in TITLE= "$1"
that causes the error. Try with TITLE="$1"
Autres conseils
In my case:
echo "Deploy of `$1` to `$2` 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.
Similar answer https://askubuntu.com/questions/180320/bash-script-program-with-parameters-as-a-single-variable-command-not-found
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow