Output redirection meaning

Hi,
Can anyone please explain the details of the code below :-

ls /etc/*.txt > /dev/null 2>&1

Both, STDERR (2) and STDOUT (1) are redirected to nowhere. It's practical when you just want to check the return status of preceding command and don't want to see any output e.g. STDOUT or STDERR.

Edit: Small improvement of my previous answer for better understanding:

ls /etc/*.txt > /dev/null 2>&1

Redirect STDOUT to /dev/null respectively don't show output; redirect STDERR to STDOUT

Hi,
Yes, I want to check the return value of a command in UNIXX shell. But, some tools say "Ambiguos Redirection". ? Any other elegant way ?

Which tools for example? Do you run your failing command from shell or from a script?

I think they are custom 3rd party tools which are present as part of OS installation. Some tools are returning long strings. So suppose I want to grep on a string like this -

"Status: XXXX"

where XXXX denotes number. How do I do that ? And, if that particular string is matched, I would want to do some operation.

I think that this is what pseudocoder would have meant by the return value:

$ ls /etc/*.txt #> /dev/null 2>&1
ls: /etc/*.txt: not found
$ ls /etc/*.txt > /dev/null 2>&1
$ echo $?
1

Where a non-zero value returned equates to an error...

curleb: yep :slight_smile:

$ ls
abc.dat
$ ls abc.dat > /dev/null 2>&1
$ echo $?
0
$ ls 123.dat > /dev/null 2>&1
$ echo $?
1
$

---------- Post updated at 11:40 ---------- Previous update was at 11:35 ----------

We would need the complete output of that particular command, than we could offer you an appropriate awk or cut command, which will e.g. use ":" as field delimiter character