Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

The following is working as expected at command prompt. But it does not work from cron.

# echo "test abc xyz" | grep "test1 \| pqr"

# echo "test abc xyz" | grep "test \| pqr"
test abc xyz

When I use this statement in the cron, I need to escape certain characters. But I am not able to figure it out.

share|improve this question
Is there a good reason not to just package that command line into a shell script file? – rackandboneman Jan 11 '13 at 10:33
up vote 1 down vote accepted

I am not seeing any issue when I am trying to put it in cron. Are you trying to do the same like following:

[sgeorge@sgeorge-ld ~]$ crontab -l
* * * * * /bin/echo "test abc xyz" | grep "test \| pqr" >> stack
* * * * * /bin/echo "test abc xyz" | grep "test1 \| pqr" >> stack_with_1

[sgeorge@sgeorge-ld ~]$ ls -l stack*
-rw-r--r-- 1 sgeorge eng 13 Jan 11 02:45 stack
-rw-r--r-- 1 sgeorge eng  0 Jan 11 02:45 stack_with_1

[sgeorge@sgeorge-ld ~]$ cat stack_with_1 
[sgeorge@sgeorge-ld ~]$ cat stack
test abc xyz
share|improve this answer
In my case it works only if there is no space. for e.g. "test1\|pqr". But you are correct, there is no need of escaping in this case. – shantanuo Jan 11 '13 at 11:22

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.