Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to get a list of the running services. I ran

service --status-all

It will return something like

 [ + ]  acpid
 [ ? ]  alsa-utils
 [ + ]  apache2
 [ + ]  atd
 [ + ]  avahi-daemon
 [ ? ]  binfmt-support
 [ + ]  bluetooth
 [ - ]  bootlogd
 [ - ]  bootlogs

I try to grep and it tries to work with the special characters.

sudo service --status-all | grep '+'

Is there anything that i can do to get it to work correctly.

share|improve this question
try with sudo service --status-all | grep -L '+' because the ' already says that it won't be escape character... – poz2k4444 Jan 17 '13 at 2:02
up vote 6 down vote accepted

I try to grep and it tries to work with the special characters.

With basic regular expressions (BRE) enabled (the default in absence of the switches -E, -F or -P), grep interprets the plus character as a literal +.

The problem is that only lines that begin with [ + ] or [ - ] get sent to STDOUT, while lines that begin with [ ? ] get sent to STDERR. Only STDOUT gets piped to grep; STDERR gets printed directly on the terminal.

To obtain the output you're expecting, execute the following command:

service --status-all 2>&1 | grep '+'

2>&1 sends STDERR to STDOUT, so grep is able to filter it.

Since you're printing only lines begnning with  [ + ]  , you might as well filter that part out. To do so, you can use the -o switch to print only matched output, the -P switch to enable Perl Compatible Regular Expressions (PCRE) and a look-behind assertion ((?<=...)):

service --status-all 2>&1 | grep -Po '(?<= \[\ \+ \]  ).*'

Note that with PCRE, you have to escape the plus sign and the brackets with a backslash.

The same result can be achieved with BRE, without look-behinds and without knowing the exact number of spaces if you use sed:

service --status-all 2>&1 | grep '+' | sed 's/.*] *//'

Here, the BRE .*] * (everything up to the right bracket, followed by any number of spaces) gets replaced by the empty string.

share|improve this answer
thanks great answer is there any way to remove the [ + ] – WojonsTech Jan 17 '13 at 2:22
@WojonsTech: I've updated my answer. – Dennis Jan 17 '13 at 2:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.