brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: apt-config shell reads apt settings into shell variables

Apt
~/tips/tip-apt-config-shell-variables

apt-config shell VARIABLE APT::Config::Key [VARIABLE2 Key2 ...]

Turns apt configuration values into shell variables you can eval in a script.

I don’t like hard-coding /var/cache/apt in a maintenance script when apt already knows where its cache lives, and the answer can differ per machine. apt-config has a second mode besides dump that exists for exactly this: it hands apt’s settings to a shell.

apt-config shell VARIABLE APT::Config::Key [VARIABLE2 Key2 ...]

You give it pairs: a shell variable name and the apt configuration key to look up. It prints shell assignments, one per pair, and you eval them. A key can end in /f, /d, /b or /i. According to the apt-config man page, f returns file names, d directories, b true or false and i an integer, each one normalized and verified.

eval "$(apt-config shell ARCH APT::Architecture CACHE 'Dir::Cache/d')"
echo "$CACHE $ARCH"

On Ubuntu 24.04 the first command printed nothing by itself and the echo gave /var/cache/apt/ amd64. Without the eval, the same command shows what apt-config really outputs: ARCH='amd64' and CACHE='/var/cache/apt/'. Note the trailing slash on the directory, which comes from the /d normalization.

The /b suffix is handy for switches. apt-config shell RECS 'APT::Install-Recommends/b' printed RECS='true' on the same machine, so your script can test the real apt setting instead of guessing.

Two caveats. The arguments must come in pairs: giving a key without a variable name fails with E: Arguments not in pairs. And you are evaluating output as shell code, so only feed it keys you typed yourself, never a key taken from user input. If you only want to look at a value by hand, apt-config dump is the better tool, since it shows the whole configuration tree.

Further reading: apt-config(8) man page

More Apt tips sudo apt-mark minimize-manual apt list '~c' grep -E ' (install|upgrade|remove) ' /var/log/dpkg.log | tail -o Dpkg::Options::="–force-confold" upgrade sudo apt-get -d -y full-upgrade All Apt tips (43) All topics

Link