0
if [[ "$PROXY_URL"==https* ]]; then
    echo "Woohoo"
else
    echo "Woohoo"
fi

Running $PROXY_URL = "https://yolo" ; ./proxyEnv.sh gives me output of:

bash: =: command not found
Woohoo

What does the "bash: =: command not found" refer to?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
  • beware of spaces: `varname=value`. The same goes for condition inside `[[ ]]`: put a space after the `==` – Derlin Jun 27 '17 at 20:42
  • Same issue with `if [[ "$PROXY_URL"== https* ]]; then` – Chris Stryczynski Jun 27 '17 at 20:44
  • In addition to not having spaces, the _assignment_ should not have `$` and _should_ have `export` (or `declare/typeset -x`) since you want it available to a different script. – dave_thompson_085 Jun 27 '17 at 20:48
  • 3
    http://shellcheck.net/ will identify your problem here. – Charles Duffy Jun 27 '17 at 20:48
  • 1
    ...it's also [BashPitfalls #16](http://mywiki.wooledge.org/BashPitfalls#foo_.3D_bar). And your script is, further, falling into [BashPitfalls #10](http://mywiki.wooledge.org/BashPitfalls#if_.5Bbar.3D.22.24foo.22.5D.3B_then_...). – Charles Duffy Jun 27 '17 at 20:51

1 Answers1

2

Your string comparison should have spaces around the comparator:

if [[ "$PROXY_URL" == https* ]]; then
    echo "Woohoo https"
else
    echo "Woohoo no https"
fi

Also, that's not how you pass environment variables to bash scripts. You have two options:

PROXY_URL="https://yolo" ./proxyEnv.sh

or

export PROXY_URL="https://yolo"; ./proxyEnv.sh

The first option assigns (without the $) the value to the symbol and then uses that environment for the script (without the ; separating them). It only exists for the script.

The second option exports that symbol to the current environment, which the script inherits.

msanford
  • 11,803
  • 11
  • 66
  • 93