You are not logged in.

#1 2021-10-27 05:23:30

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,353

BASH: prevent output from going to STDERR

example script

#!/bin/bash

echo "hello"

select ops in 1 2 3 4 5; do
	echo $ops
done

Now let's run this

[ins] $ bash test 2> /dev/null
hello
^C

[ins] $ bash test 1> /dev/null
1) 1
2) 2
3) 3
4) 4
5) 5
#? ^C

output of echo goes to STDOUT but output of select loop goes to STDERR

Use case: I have a opener script which uses select loop to choose  application. I am trying to use that with clifm as file-opener. Problem is clifm redirects STDERR, so that all the output from select loop becomes invisible.


Arch is home!
cwm rofi weaver clifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#2 2021-10-27 06:51:22

seth
Member
Registered: 2012-09-03
Posts: 25,134

Re: BASH: prevent output from going to STDERR

Is this a question or an "interesting find" post?
"2>&1" will redirect the loop to stdout.

Offline

#3 2021-10-27 08:25:16

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,353

Re: BASH: prevent output from going to STDERR

This is a question, as I have written in use case.
2>&1 is more like a workaround, it needs to be applied everytime I use select loop ( which is many times in my file opener script )
Is this intended feature for some weird reason or a bug in bash?


Arch is home!
cwm rofi weaver clifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#4 2021-10-27 11:37:18

seth
Member
Registered: 2012-09-03
Posts: 25,134

Re: BASH: prevent output from going to STDERR

English questions typically invert verb and subject…
https://www.thoughtco.com/question-grammar-1691710

----

On topic: Because the typical usecase of "select" will be an interactive one where you ask the user something and then do something w/ the result and that something might be redirected (as in your demo to /dev/null, just more usable) I would assume using stderr is perfectly deliberate here and sane behavior in terms of what users would expect and the split certainly allows to distinguish between the UI and the actual input.

It's not a bug for sure, because

https://man.archlinux.org/man/core/bash/bash.1.en wrote:

select name [ in word ] ; do list ; done
The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed (see PARAMETERS below). The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed. The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.

Offline

#5 2021-10-27 12:41:21

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,353

Re: BASH: prevent output from going to STDERR

I see, I need to spell out the question and put a question mark.

---

On the topic: As per my rudimentary understanding of the English language, question and error, have different meanings. With the select loop what we are doing is asking a user for his choice, which is a question not error.

But as I see this is in bash manual, so it has to be a feature. The problem is the script I am using to open files has no intention of redirecting output of select loop to somewhere but the file manager (clifm) with which I am using my file opener script is redirecting all STDERR to some unknown place.

You see automatically dividing all output in STDOUT or STDERR is not a very good solution. If you has vis installed (another vim like file manager) try this

vis 2>/dev/null

all output of vis is STDERR


Arch is home!
cwm rofi weaver clifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

#6 2021-10-27 12:59:31

Maniaxx
Member
From: .de
Registered: 2014-05-14
Posts: 489

Re: BASH: prevent output from going to STDERR

You can put 'exec 2>&1' in your script so it gets redirected automatically.


sys2064

Offline

#7 2021-10-27 13:22:00

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,353

Re: BASH: prevent output from going to STDERR

Thanks everyone, I think I will ask clifm maintainer about this.


Arch is home!
cwm rofi weaver clifm vis lizzy pass terminator
https://github.com/Docbroke

Offline

Board footer

Powered by FluxBB