I am writing a script that needs to read a file containing information of a package for which I wrote this line

apt show $PACKAGE_NAME > pack_info.txt

However this doesn't creates the pack_info.txt file and always gives this Warning :

WARNING : apt does not have a stable CLI interface. Use with caution in scripts.

For the time being I tried redirecting both STDOUT & STDERR using &> and it worked out to give a file pack_info.txt that we need.

I also tried getting contents from dpkg and that also worked :

dpkg -s $PACKAGE_NAME > pack_info.txt

What is good that it neither shows a warning nor an error.

What is bad that we don't want to use dpkg and only want STDOUT of apt to redirect to file.

So, I have three Questions to ask :

  1. What do we exactly mean by Stable CLI Interface ?
  2. How to safely and error-free use such commands in scripts ? [please care to explain with example]
  3. Is there a way to only and only redirect STDOUT of apt show to a file ?

apt is for the terminal and gives beautiful output while apt-get and apt-cache are for scripts and give stable, parsable output. The script equivalent of your apt show command therefore is:

apt-cache show $PACKAGE_NAME >pack_info.txt

Now to answer your questions one by one:

What do we exactly mean by Stable CLI Interface?

apt's output is not well usable in scripts. For example, apt install (compared to apt-get install) displays a progress bar that's useless for scripts and can throw errors when the output is parsed. apt show firefox shows a hint for an additional record, which is also totally useless in a script, you want it to simply output every record there – that's what apt-cache show firefox does. Let's see what man apt has to say about that:

The apt(8) commandline is designed as an end-user tool and it may change behavior between versions. While it tries not to break backward compatibility this is not guaranteed either if a change seems beneficial for interactive use.

All features of apt(8) are available in dedicated APT tools like apt-get(8) and apt-cache(8) as well. apt(8) just changes the default value of some options (see apt.conf(5) and specifically the Binary scope). So you should prefer using these commands (potentially with some additional options enabled) in your scripts as they keep backward compatibility as much as possible.

How to safely and error-free use such commands in scripts?

Just use apt-get or apt-cache respectively instead of plain apt. :) See this answer for a list of equivalents.

Is there a way to only and only redirect STDOUT of apt show to a file?

You did that correctly already: >file or 1>file redirects stdout, 2>file redirects stderr and &>file redirects both to file.


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.