Search code examples
bashunixwcgrep

bash -gt command with pgrep & wc


I'm trying to run [ pgrep mongo | wc -l -gt 2] to see if there are more than 2 mongo processes running, but I keep getting this error -bash: [: missing `]'

I feel like I'm missing something simple here. Thanks!


Solution

  • You need command substitution and a space before ]:

    [ $(pgrep mongo | wc -l) -gt 2 ]
    

    $(...) is the syntax for command substitution