12

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?

3
  • Perhaps you meant to write TITLE="$1" - that space really matters! Commented Jun 18, 2019 at 14:42
  • @TobySpeight that's what the accepted answer suggested in 2012 Commented Jun 19, 2019 at 19:07
  • so it does - it's a pity the review UI doesn't show answers... Commented Jun 19, 2019 at 20:17

2 Answers 2

23

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

Sign up to request clarification or add additional context in comments.

1 Comment

Definitely is. VAR=VALUE PROGRAM defines a variable VAR for the environment of PROGRAM only. In the OP's case the shell interprets "$1" as a command and TITLE= as the definition of an (empty) environment variable for it.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.