String comparison in bash. [[: not found

后端 未结 7 1348
故里飘歌
故里飘歌 2020-11-29 17:40

I am trying to compare strings in bash. I already found an answer on how to do it on stackoverflow. In script I am trying, I am using the code submitted by Adam in the menti

相关标签:
7条回答
  • 2020-11-29 18:23

    I had this problem when installing Heroku Toolbelt

    This is how I solved the problem

    $ ls -l /bin/sh
    lrwxrwxrwx 1 root root 4 ago 15  2012 /bin/sh -> dash
    

    As you can see, /bin/sh is a link to "dash" (not bash), and [[ is bash syntactic sugarness. So I just replaced the link to /bin/bash. Careful using rm like this in your system!

    $ sudo rm /bin/sh
    $ sudo ln -s /bin/bash /bin/sh
    
    0 讨论(0)
  • 2020-11-29 18:23

    Specify bash instead of sh when running the script. I personally noticed they are different under ubuntu 12.10:

    bash script.sh arg0 ... argn

    0 讨论(0)
  • 2020-11-29 18:24

    Is the first line in your script:

    #!/bin/bash
    

    or

    #!/bin/sh
    

    the sh shell produces this error messages, not bash

    0 讨论(0)
  • 2020-11-29 18:24

    If you know you're on bash, and still get this error, make sure you write the if with spaces.

    [[1==1]] # This outputs error
    
    [[ 1==1 ]] # OK
    
    0 讨论(0)
  • 2020-11-29 18:28

    How you are running your script? If you did with

    $ sh myscript
    

    you should try:

    $ bash myscript
    

    or, if the script is executable:

    $ ./myscript
    

    sh and bash are two different shells. While in the first case you are passing your script as an argument to the sh interpreter, in the second case you decide on the very first line which interpreter will be used.

    0 讨论(0)
  • 2020-11-29 18:40

    As @Ansgar mentioned, [[ is a bashism, ie built into Bash and not available for other shells. If you want your script to be portable, use [. Comparisons will also need a different syntax: change == to =.

    0 讨论(0)
提交回复
热议问题