2

I'm a beginner to Cygwin. If I have:

cat output.txt | rm output.txt

I want to print out the results before I delete the file, but the results don't print even though these commands work find when inputted into Cygwin separately. Similarly:

grep -c 'ps' count.tmp | rm count.tmp

I want to show the results of grep -c 'ps' count.tmp before the count.tmp file is removed.

2 Answers 2

3

Don't pipe them.

Do this instead:

cat output.txt ; rm output.txt
grep -c 'ps' count.tmp ; rm count.tmp

Edit: The pipe (|) takes the standard output of one command and runs it into the standard input of another... like this:

# Get the number of lines
cat output.txt | wc -l 

or 

echo "Hello there" | tr -d' '

To JOIN commands, use either ; or &&

The ; will run the commands one after the other

echo Hi ; false; echo there

The && will run the next command only if the previous command exited with a zero status code (success).

echo Hi && false && echo there
0

Piped commands can be run in any order, so there is no guarantee that the rm command won't be run first. Use

cat output.txt && rm output.txt

which will only remove the file if the cat command succeeds.

1
  • 1
    This is an important distinction for people coming from a Windows/DOS background, where piped commands do run in order, passing stdio around in temporary files. Sep 18, 2012 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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