Home > Software design >  how to fix `command not found` in bash scripting?
how to fix `command not found` in bash scripting?

Time:12-10

I want to create a bash function to load certain environment variables when called, but I'm getting the error loadenv:4: = not found. this function, along with the variables DEV_ENVIRONMENT_NAME, DEV_ENVIRONMENT_DIRECTORY, PROD_ENVIRONMENT_NAME and PROD_ENVIRONMENT_DIRECTORY are defined within my .zshrc file so the exported variables are available in the bash session I run the function in. But I don't know what it means by the error I mentioned.

function loadenv() {
  environment=$1
  envname=""
  envdir=""
  if [ "$environment" == "dev" ]
  then
    echo "Assuming development credentials"
    envname="$DEV_ENVIRONMENT_NAME"
    envdir="$DEV_ENVIRONMENT_DIRECTORY"
  elif [ "$environment" == "prod" ]
  then
    echo "Assuming production credentials"
    envname="$PROD_ENVIRONMENT_NAME"
    envdir="$PROD_ENVIRONMENT_DIRECTORY"
  fi
  if [[ -z $envname || -z $envdir ]]
  then
    echo "Credentials for $environment not properly configured"
    return 1
  else
    export APP_ENVIRONMENT="$envname"
    export APP_DIRECTORY="$envdir"
    return 0
  fi

  echo "Environment '$environment' not valid"
  return 1
}

CodePudding user response:

The error comes from the fact that the two forms of bash logical expressions are either (single brackets with single "="),

if [ "$environment" = "dev" ]

or (double brackets with "==" ),

if [[ "$environment" == "dev" ]]

If that script is meant to be , then you need to have

#!/bin/bash

as the first line in your script, for it to work, regardless of the environment.

Also, be sure to NOT source that script into your zsh. Otherwise, it will not execute as bash.

  • Related