The output of the command to the command ':'

I am updating an old script and came across a template that I am not familiar with:

# NOTE The | : always returns true so the <cmd> doesn't fail <cmd> | : 

I have ever seen this sample used in the fork example. If someone asked me how to accomplish what is stated in the comment, I would suggest:

 <cmd> ||: 

Before I remember this before the typo, did anyone see this sample and explain its use case?

+7
bash pipe sigpipe colon
source share
2 answers

This is a typo that also has a similar effect. Differences:

  • cmd | : cmd | : pipe cmd stdout to : Since : exit immediately if cmd writes everything that is most likely to be hit by a SIGPIPE signal or an EPIPE error, usually killing it. †
  • cmd | : cmd | : runs cmd in a subshell, invalidating environmental changes such as var=value or cd /dir . Compare cd /tmp || : cd /tmp || : with cd /tmp | : cd /tmp | : .
  • cmd | : cmd | : will not work if set -o pipefail enabled.

Based on the comment should be || : || : .

† Technically, this is a race condition. cmd may write something before the release : although this is unlikely. Or, even more unlikely, if cmd wrote a lot and filled the buffer buffer, it would actually block until the exit : after which its waiting syscall write() will receive EPIPE / SIGPIPE. You can simulate this with strace -e write yes | { sleep 0.1; :; } strace -e write yes | { sleep 0.1; :; }

+7
source share

I think this is a way to suppress the standard output of the command, this is equivalent:

 <cmd> > /dev/null 

I see this is useful in scenarios where you don't want to show cmd output

-one
source share

All Articles