-1

I'm doing the following assignment in my bash script but keep receiving weird errors when not doing it inline.

Original that works

$DB_ENV = $(echo "$(cat .dbenv)" | tr '\n' ' ')
echo "CACERT=\"$(cat .cacert)\" $DB_ENV" yarn run knex migrate:up | bash -

New that doesn't

#!/bin/bash

$DB_ENV=$(echo $(cat .env) | tr '\n' ' ')
$CACERT="CACERT=\"$(cat .cacert)\""

if [[ $1 != 'up' && $1 != 'down' ]]; then
  echo Available subcommands are "up" and "down"
else
  echo "$CACERT $DB_ENV yarn run knex migrate:$1" | bash -
fi

For some reason, on the first variable assignment line I'm getting

migrate.sh: line 3: =NODE_ENV="production": command not found

and on the second

migrate.sh: line 4: =CACERT="<file contents with newlines>": File name too long

Genuinely confused as to why this is happening, any help would be great!

Jack
  • 955
  • 1
  • 9
  • 30
  • 1
    are you sure your `Original that works` **really** works? consider cutting-n-pasting your code into [shellcheck.net](https://www.shellcheck.net/) for some recommendations – markp-fuso Aug 17 '21 at 14:55
  • If the first snippet worked, you are not using Bash. Either way, you want to get rid of the [useless `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) and the [useless `echo`](http://www.iki.fi/era/unix/award.html#echo). – tripleee Aug 17 '21 at 15:02
  • You are attempting to pass the contents of `CACERT` as a command to Bash for execution. That makes no sense, to the point that we cannot guess what you are hoping this code should do instead. – tripleee Aug 17 '21 at 15:07

1 Answers1

1

You have at least two erros:

  • You should not have $ in front of variables when assigning to them.
  • Do not put a space on either side of assignments =.

As an example, your first line should be

DB_ENV=$(echo "$(cat .dbenv)" | tr '\n' ' ')
jmd_dk
  • 12,125
  • 9
  • 63
  • 94