1

I noted apt output is different when executed alone in Bash shell and when output is redirected to files.

For example:

$ apt install ./*.deb --simulate 1>111.txt 2>222.txt

adding redirection results in text WARNING: apt does not have a stable CLI interface. Use with caution in scripts. in file for stream 2, whereas running apt w/out redirection does not display this text in shell window.

Why output depends on redirection? Maybe apt sees redirection as its` parameter? How can I write redirection to be unnoticed to other part of shell command line?

P.S. I saw it in Ubuntu 18 and apt is specific app, but maybe the issue is general to Unix, so I put only shell in tag.

2

apt has been designed historically with interactive command-line use in mind, hence the coded warning, which has been done on purpose in/by apt and not by the shell.

However, apt is not necessarily parsing the command line; the shell has already done that job by the time it calls apt. What apt is doing is detecting that the stdout stream has been changed/redirected.

As for using apt in scripts, you have got the similar older command apt-get which does does more or less the same thing, and does not give that warning when stdout is redirected.

TLDR Is not the shell writing the error message in stderr, it is the apt command.

The fact that you are able to capture the apt stderr output in the shell is an artifact of Unix being a multitasking OS, and does not mean it is the shell producing that output.

PS. Doing an strace at the command, it can be seen apt writing that message:

strace apt get install bash > a
....
write(2, "\n", 1
)                       = 1
write(2, "WARNING: ", 9WARNING: )                = 9
write(2, "apt", 3apt)                      = 3
write(2, " ", 1 )                        = 1
write(2, "does not have a stable CLI inter"..., 38does not have a stable CLI interface. ) = 38
write(2, "Use with caution in scripts.", 28Use with caution in scripts.) = 28
write(2, "\n", 1
)                       = 1
write(2, "\n", 1
)                       = 1
  • Is there any way to prevent e.g. apt from detecting redirection? – Alexei Martianov Aug 4 '18 at 14:21
  • 1
    The canonical way is using the equivalent tools like apt-get and apt-cache. No switch/option to disable that behaviour that I know of. – Rui F Ribeiro Aug 4 '18 at 14:24
  • What bothers me is not of cause that single additional phrase from apt, but the whole thing that when I see a long output in shell, I'd like to store it, do text search later, but cannot save it 100% the way it displays. – Alexei Martianov Aug 4 '18 at 14:30
  • 1
    That is a whole different question actually. There is the script command for saving sessions of work. – Rui F Ribeiro Aug 4 '18 at 14:38
  • script is what I can use, thanx! – Alexei Martianov Aug 4 '18 at 15:42
0

"How can I write redirection to be unnoticed to other part of shell command line?"

You don't have to do anything, the warning is on stdErr. Try this:

apt list adghdgd | grep .
2>/dev/null apt list adghdgd | grep .

If your distribution colors grep by default, you will see that the part that actually goes to grep gets colored. In the second form stdErr gets suppressed alltoghether.

Grep also behaves differently, compare this:

echo hello | grep .
echo hello | grep . | cat

Second form is not colored, because grep notices its stdOut is not a terminal, but a pipe. How does he/she knows????

Compare

ls -l /proc/self/fd/1
( ls -l /proc/self/fd/1 ) | cat

In the first form stdOut is tty/pts. In the second, a pipe.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.