Confused why using -c when checking the output? It's used to check how many times something is matched - not if it's successful or not.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines. (-c is specified by POSIX.)
but in this example
check="$(grep --silent -i 'text' file.sh; echo $?)"
It doesn't output anything except a exit code, which is then echoed. This is the output that the variable check uses. I also prefer it because it's a single line.
You can replace --silent with -q. I use it since you're not interested in the grep output, just whether it worked or not.
-q, --quiet, --silent
Quiet; do not write anything to standard output. Exit
immediately with zero status if any match is found, even if an
error was detected. Also see the -s or --no-messages option.
(-q is specified by POSIX.)
$ check=$(echo test | grep -qi test; echo $?) # check variable is now set
$ echo $check
0
$ check=$(echo null | grep -qi test; echo $?)
$ echo $check
1
$?
right after command is finished. – coffeMug Jul 7 at 8:16