Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash shell if operator

I write a function like this to .commands file, and I imported .commands in .zshrc

function testA() {
  echo "start"
  if [ "$1" == "a" ]; then
    echo "ok"
  fi
  echo "end"
}

And when I run testA a in terminal

start
testA:2: = not found

What is the problem here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Result

>Solution :

Chapter 12 – "Conditional Expressions" of the zsh documentation states:

A conditional expression is used with the [[ compound command to test attributes of files and to compare strings.

This means, changing your conditional expression to use [[ ... ]] instead of [ ... ] should make it work:

function testA() {
  echo "start"
  if [[ "$1" == "a" ]]; then
    echo "ok"
  fi
  echo "end"
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading