dig +short: Scriptable DNS Answers in CI
dig +short strips dig output down to the bare answer values, so a script can capture an address directly.
When a pipeline needs the IP a name points to, +short gives exactly that and nothing else. It is the form to use in command substitution where the verbose default output would get in the way.
What it does
dig +short suppresses every section except the answer values, printing one record per line. For an A query that is the IP addresses; for CNAME or MX it is the targets. An empty output means no record of that type exists.
Common usage
# capture the first resolved address
IP=$(dig +short api.example.com A | head -n1)
# list all A records
dig +short api.example.com A
# answer section with TTLs (more than +short, less than full)
dig +noall +answer api.example.comOptions
| Flag | What it does |
|---|---|
| +short | Print only the answer values |
| +noall +answer | Print the answer section with names and TTLs |
| +nocomments | Drop the comment lines from full output |
| head -n1 | Take the first answer when several are returned |
In CI
A CNAME makes +short print the alias target as well as the final IP, so a script expecting one line can get several; filter with grep or take the last line for the address. An empty +short result is a normal "no such record", so test for an empty string rather than relying on the exit code alone.
Common errors in CI
An empty result where you expected an IP often means you queried the wrong type (A vs AAAA) or the default resolver failed silently; rerun without +short to see the status. If $(dig +short ...) captures multiple lines, the variable holds them all, which breaks a later host:port string; pipe through head -n1.