I just noticed that no matter which cryptsetup
FDs are forwarded to /dev/null
it still shows prompt for password. For example this still shows prompt:
cryptsetyp luksOpen /dev/sdXY name >/dev/null 2>/dev/null
How can you display in terminal message that cannot be redirected to file using standard redirection?
I'd like to get such functionality in bash script as I use stdout to return result to mother script but I'd still like to display interactive prompt – is it possible to do so using bash?
Best Answer
Presumably, It writes directly to
/dev/tty
(at any rate, you can get the same behavior)alternatively, you can simply do:
read
still works if you do this.The exec is required to keep the redirection for the duration of the present shell.
A redirection on a simple command:
lasts while the command is being executed. Once the command (echo in this example) ends, the shell removes the redirection and reverts back to the "present shell" execution environment.
A:
is still a "simple command" where the command executed is "none", the redirection will not live for long.
Instead in:
the exec replaces the "present shell" with a new one which includes the redirection. That makes the redirection stay alive for as long as the "present shell" exists. That could be undone (well, actually close fd 3) with: