skip to Main Content

What does ‘[‘ ‘]’ indicate in this output?

$ bash -ex ~/bin/client_services 
+ : starting daemons reqd. for clients
++ ps aux
++ grep -q memcached
+ '[' ']'

My source file is:

if [ `ps aux | grep -q memcached` ]; then
  echo 'Memcached exists'
fi

2

Answers


  1. What you’re saying here is the output of the debugging -x flag for bash. First it has to run the ps aux, then it has to run the grep .... Then it has to test the results. The [...] test syntax is an expression in itself, and also has to be evaluated.

    The + '[' ']' is the representation of that test being performed, shown to you by bash -x.

    Login or Signup to reply.
  2. It means ps aux | grep -q memcached expanded to an empty string.

    If it didn’t you’d get what it expanded to.

    E.g.,
    ps aux | grep -q memcached prints

    ++ echo true
    + '[' true ']'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search