0

I am working on scripting and using bash shell for execution. Following are two different variations that I am trying to execute. but its not working not on execution there is error on console for double bracket in if condition.

#!/bin/bash

STR='GNU/Linux is an operating system'
    SUB='Linux'
    if [[ "$STR" == *"$SUB"* ]]
    then
      echo "It's there."
    else
        echo "Not It's there."
    fi  
    

    if [[ $STR =~ $SUB ]]; then
        echo found
    else
        echo not found
    fi

enter image description here

I took the reference of this example from https://linuxize.com/post/how-to-check-if-string-contains-substring-in-bash/ . now not sure why its not working. But in my project, following syntax is working fine. Here I was checking for equality which works fine but in above example I want to verify contains.

if [ $1= "test" ]
    then
     test=true
    elif [ $1= "dev ]
    then
     dev=true
    fi

Bash Version

enter image description here

OS

enter image description here

can someone help me ?

13
  • what do you mean by this @Cyrus. I did not get any answer yet Commented Dec 5, 2021 at 7:01
  • Does everything work when you remove the forward slash in "GNU/Linux"? Commented Dec 5, 2021 at 7:02
  • @DavidGrayson, unfortunately not. it complains on double bracket in if condition. I had similar issue with the last example and I had removed double brackets [[ to single [. Commented Dec 5, 2021 at 7:05
  • 2
    I just tried your code on Bash 4.4 in Linux and Bash 5.1.8 in MSYS2 and it worked fine. I copied and pasted it into a script named test.sh, marked it as executable, and then ran ./test.sh. The output was It's there followed by found. Could you give more details about your environment and exactly how you are running code? Did you copy and paste the exact code that you are running into this question? Commented Dec 5, 2021 at 7:11
  • 1
    $SHELL doesn't necessarily reflect the shell you're actually running at the time; it's just your preferred one. Commented Dec 5, 2021 at 7:40

1 Answer 1

3

I think you have two problems:

First, you're running the scripts with sh, not bash.

Second, [ $1= "test" ] is invalid syntax (in either shell). There must be a space before =. If you do [ $1= "test" ] in sh you will get the error unexpected operator (as in your screen capture). Whereas bash will print unary operator expected.

Double square brackets ([[) and globs (*"$SUB"*) in string matching are bash syntax, and are not available in sh. If you run the first if statement from your first example in sh, it will not print an error, but it will always evaluate to false (printing Not It's there.), wether the substring matches or not, due to [[.

So, you should fix the syntax errors: [ $1= "test" ] should be [ "$1" = "test" ].

You should also run your script with bash, not sh, to use bash specific syntax:

You can call the script like bash script-file or bash /path/to/script-file.

Or, make sure the script is executable: chmod a+x script-file, and call it with a full path: ./script-file or /path/to/script-file.

Your shebang (#!/bin/bash) is correct if on Linux. On another platform you can try #!/usr/bin/env bash.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.