I'm running a command like so, with the $OUTPUT variable saving the results of the command. But I also want to save the command itself to a variable for inclusion in a status email.

OUTPUT=$(php -f $LOCATION/somefile.php -- -process "$INPUTFILE" 2>&1)

The first part works. Then I tried this:

IMPORTCOMMAND='php -f' "$LOCATION"'/somefile.php -- -process'"$INPUTFILE"

But instead of saving the string to the variable, it seems to be just executing the command a second time.


Here is a mockup of how I create my email body. I have single quotes around regular strings and then double quotes around bash variables.

BODY='<b color="red">Output:</b><br />'"$OUTPUT"'<b color="red">Command:</b> '"$IMPORTCOMMAND"

After that I try to replace newlines with html "
" like so:

BODY="${BODY//$'\n'/<br />}"
#changed $BODY="" to BODY="" per yaegashi's suggestion

The following error disappeared after I followed yaegashi's suggestion:

/usr/local/bin/some-script.sh: line 59: <b: command not found

But the original error remains, at the IMPORTCOMMAND variable assignment.

  • 2
    You need to double quote "$IMPORTCOMMAND" when using it. – cuonglm Jul 23 '15 at 2:00
  • @cuonglm thanks for the hint. I did double quote it when I assigned it to my $BODY variable. It seems like the error occurs when I try to replace newlines with <br />. Please see my edit. – Buttle Butkus Jul 23 '15 at 2:13
  • 1
    You should do like BODY="...". No heading $. – yaegashi Jul 23 '15 at 2:20
  • @yaegashi I'm getting an error at the line IMPORTCOMMAND='php -f' "$LOCATION"'/somefile.php -- -process'"$INPUTFILE" line. The php script is being executed. I must be doing something wrong with the quotes up there. – Buttle Butkus Jul 23 '15 at 2:33
  • 1
    just a matter of spacing; I think you want something like: IMPORTCOMMAND='php -f '"$LOCATION"'/somefile.php -- -process '"$INPUTFILE" – Jeff Schaller Jul 23 '15 at 3:01

I think you have too many quotes and in the wrong places.

The following will try run somefile.php and not set IMPORTCOMMAND as there is a space between php -f and somefile.php.

IMPORTCOMMAND='php -f' "$LOCATION"'/somefile.php -- -process'"$INPUTFILE"

Should be:

import_command="php -f $LOCATION/somefile.php -- -process $INPUTFILE"
output=$( ${import_command} 2>&1 )

OR (if 'LOCATION' or 'INPUTFILE' contain spaces)

import_command=( php -f "$LOCATION/somefile.php" -- -process "$INPUTFILE" )
output=$( "${import_command[@]}" 2>&1 )

And display it:

BODY="<b color='red'>Output:</b><br />${output}
      <b color='red'>Command:</b>     ${import_command[*]}"
  • The space after 'php -f' was indeed the problem. You and Jeff Schaller are correct. $LOCATION and $INPUTFILE are both file paths with no spaces, but it's good to know how to handle spaces for future reference. Thank you. – Buttle Butkus Jul 23 '15 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.