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: update-alternatives --query prints the state of one alternative in a parseable format

Linux
~/tips/tip-linux-update-alternatives-query

update-alternatives --query awk

Shows whether an alternative is in auto or manual mode and which target it uses.

Before I change an alternative, I want to know whether someone already did. update-alternatives --config answers that only by dropping you into a menu. The query mode just reports.

update-alternatives --query awk

The man page says --query displays information about the link group like --display, but in a machine-parseable way. On Ubuntu 24.04 with mawk installed, it printed this:

Name: awk
Link: /usr/bin/awk
Slaves:
 awk.1.gz /usr/share/man/man1/awk.1.gz
 nawk /usr/bin/nawk
 nawk.1.gz /usr/share/man/man1/nawk.1.gz
Status: auto
Best: /usr/bin/mawk
Value: /usr/bin/mawk

Alternative: /usr/bin/mawk
Priority: 5
Slaves:
 ...

The fields that matter are Status (auto or manual), Best (the path the system would pick on its own) and Value (what the link points to right now). If they differ, or the status is manual, an administrator chose something.

For scripts, take one field:

update-alternatives --query awk | awk '/^Value:/{print $2}'
update-alternatives --get-selections | grep manual

The first line printed /usr/bin/mawk here. --get-selections lists every alternative with its status and current choice, so the second line shows everything that was set by hand. A manual choice sticks: in manual mode the system keeps the administrator’s selection, and to go back to the automatic pick you run update-alternatives --auto awk.

Caveat: for a name that doesn’t exist, the command fails with no alternatives for that name and exit status 2. The Value field can be none when the link doesn’t exist, so check for that before you use it as a path.

Further reading: update-alternatives(1) man page

More Linux tips notify-send -r "$id" "Backup" "Done" pgrep -af 'sleep 301' smbstatus -L msmtp --serverinfo --tls --tls-certcheck=off --host=smtp.example.com --port=587 smbclient //server/share -U user -Tc backup.tar "*" All Linux tips (283) All topics

Link