I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
What's wrong in my code?
I want to check if a file contains a specific string or not in bash. I used this script, but it doesn't work:
if [[ 'grep 'SomeString' $File' ]];then
# Some Actions
fi
What's wrong in my code?
Shortest (correct) version:
can be also written as
but you dont need to explicitly test it in this case, so the same with:
You don't need
[[ ]]
here. Just run the command directly. Add-q
option when you don't need the string displayed when it was found.The
grep
command returns 0 or 1 in the exit code depending on the result of search. 0 if something was found; 1 otherwise.You can specify commands as an condition of
if
. If the command returns 0 in its exitcode that means that the condition is true; otherwise false.As you can see you run here the programs directly. No additional
[]
or[[]]
.