Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

This is currently deleting the whole contents of the file in question:

cat $dir | grep -v "Listen $rpc_port" > $dir

The file contents:

Listen 9200
Listen 9201
Listen 9202
Listen 9203
Listen 9204

Could someone please either fix this or give me a working alternative?

share|improve this question
The pipe from cat to grep is a bit useless here, as you could have done grep pattern $dir – Bernhard Aug 6 '12 at 6:17
up vote 4 down vote accepted

You cannot write (=redirect) to the same file you are reading from. Either redirect to a different file, or use sed.

sed -i "/$rpc_port/d" $dir
share|improve this answer
ah, I see :P tyvm for your help, that works great :D – King Aug 6 '12 at 5:40

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.