maudev
maudev

Reputation: 1101

How to redirect stderr to stdout with a string separator

I need to execute a list of commands and separate them with a unique string separator:

ls -l 2>&1 && echo "__UNIQUE_SEPARATOR" && sls -l  2>&1 && echo "__UNIQUE_SEPARATOR" && pwd

In order to process it and split by outputs, but for some reason after stderr redirection when the command fails it doesn't printing the next string separator:

maulinux@ubuntu:~$ ls -l 2>&1 && echo "__UNIQUE_SEPARATOR" && sls -l  2>&1 && echo "__UNIQUE_SEPARATOR" && pwd
total 40
-rw-rw-r-- 1 maulinux maulinux   29 Oct  7 20:22 '&1'
drwxr-xr-x 5 maulinux maulinux 4096 Sep 26 15:22  Desktop
drwxr-xr-x 2 maulinux maulinux 4096 Aug 19 10:54  Documents
drwxr-xr-x 3 maulinux maulinux 4096 Aug 28 12:31  Downloads
drwxr-xr-x 2 maulinux maulinux 4096 Aug 19 10:54  Music
drwxr-xr-x 2 maulinux maulinux 4096 Aug 19 10:54  Pictures
drwxr-xr-x 2 maulinux maulinux 4096 Aug 19 10:54  Public
drwxr-xr-x 3 maulinux maulinux 4096 Aug 19 10:54  snap
drwxr-xr-x 2 maulinux maulinux 4096 Aug 19 10:54  Templates
drwxr-xr-x 2 maulinux maulinux 4096 Aug 19 10:54  Videos
__UNIQUE_SEPARATOR

Command 'sls' not found, but there are 21 similar ones.

maulinux@ubuntu:~$ 

And it's breaking the other commands execution after that, how can I get all outputs separated by the string ?

Upvotes: 0

Views: 45

Answers (1)

Barmar
Barmar

Reputation: 781068

The && operator means to only execute the remaining commands if the previous command succeeded. If the command doesn't succeed, the sequence of commands stop.

Use ; instead of && if you want to execute all the commands regardless of the success of previous ones.

ls -l 2>&1 ; echo "__UNIQUE_SEPARATOR" ; sls -l  2>&1 ; echo "__UNIQUE_SEPARATOR" ; pwd

Upvotes: 1

Related Questions