An issue with double brackets in shell scripting

I have a shell script which gets executed. In the script I have:

if [[ $variable == *something* ]]; then
   do something
fi

My issue here is that running the script returns "[[ not found".

From reading other answers, it looks like not all shells recognize double brackets, however, I am not sure what the solution is, what is the equivalent of that code?


It sounds like your shell is POSIX sh , which does not have the [[ builtin. Possible solutions include translating it to an expression that works for sh or changing the shebang line to be #!/bin/bash . Changing the shebang is generally the best solution.

You may have a POSIX sh if you're on Ubuntu, they use dash.


I know only single brackets, did you try it with only one bracket each? otherwise you could use test instead.


POSIX-standard solution:

if test echo "${variable}" | grep -q 'something' 
then
   # do something
fi

that will ALWAYS work :)

链接地址: http://www.djcxy.com/p/97046.html

上一篇: 在Qt控制台应用程序中读取和写入QProcess

下一篇: shell脚本中的双括号存在问题