dig +short: print only the answer (IP address or record data)
- tool
- dig / nslookup
- command
- dig
- flag
- +short
- default
- off (dig prints the full response with header, question and sections)
dig +short NAME [TYPE] prints only the record data of the answer section, one record per line, with no header, question, statistics or comments: dig +short example.com prints the IPv4 addresses, dig +short example.com MX prints 10 mail.example.com. style lines. It is the option to use in scripts. An empty output means no records of that type (or an error such as NXDOMAIN, which +short hides).
What each record type prints
| Type | Typical +short line | Notes |
|---|---|---|
| A / AAAA | 192.0.2.10 or 2001:db8::10 | One address per line, in the order the server returned them. |
| CNAME | target.example.net. | Printed before the addresses when you query an alias. |
| MX | 10 mail.example.com. | Preference number, then the mail host; lower numbers are tried first. |
| NS | ns1.example.com. | The authoritative name servers of the zone. |
| TXT | quoted string(s) | Long TXT records come back as several quoted strings on one line. |
| PTR (-x) | host.example.com. | The name for an address in a reverse lookup. |
| SOA | primary NS, mailbox, serial, refresh, retry, expire, minimum | All seven fields on one line. |
Host names end with a dot because they are fully qualified; strip it with sed 's/\.$//' when a program does not accept it.
Examples
dig +short example.com # A records
dig +short example.com AAAA # IPv6 addresses
dig +short example.com MX # preference and mail host
dig +short example.com TXT # quoted strings (SPF, verification)
dig +short www.example.com # a CNAME prints the alias target first, then its addresses
dig +short -x 192.0.2.10 # reverse lookup (PTR)
dig +short @1.1.1.1 example.com NS # ask a specific serverWhen the name is a CNAME, +short prints the chain in order: first the canonical names, then the addresses. In a script that needs only addresses, filter them:
dig +short www.example.com A | grep -E '^[0-9.]+$' | head -1Useful companions
| Option | Effect with +short |
|---|---|
| +identify | Adds the server address and port that gave each answer |
| +noall +answer | Not short, but only the answer section with names and TTLs: better when you need the TTL |
| +tcp | Query over TCP |
| +time=2 +tries=1 | Fail fast in scripts |
| -4 / -6 | Use only IPv4 or IPv6 transport to reach the server |
Check for errors in scripts
+short hides the response status, so NXDOMAIN, SERVFAIL and "no records" all produce empty output with exit code 0. When the difference matters, read the status instead:
dig example.com +noall +comments | grep -o 'status: [A-Z]*'The exit status follows the dig manual: 0 means a DNS response arrived (NXDOMAIN included), 1 a usage error, 8 an unreadable batch file, 9 no reply from the server and 10 an internal error. A timeout is still reported: the output contains ;; connection timed out; no servers could be reached (or ;; communications error) and the exit code is 9.
On Windows
dig comes with BIND, which does not ship current Windows builds. Use Resolve-DnsName example.com -Type MX in PowerShell or nslookup -type=MX example.com; both print full records rather than a short form, so select the field you need ((Resolve-DnsName example.com -Type A).IPAddress). For connection failures, see connection timed out; no servers could be reached.
FAQ
How do I see the TTL with dig +short?
You cannot; +short drops the name, class and TTL. Use dig NAME +noall +answer, which prints the full answer records including the TTL in the second column.
Can I make +short the default?
Yes. dig reads options from ~/.digrc before the command line, so a line containing +short there applies to every query. Override it for one query with +noshort.
What does dig +short print when the domain does not exist?
Nothing. NXDOMAIN is a valid DNS response, so dig exits with 0 and prints no lines. Query the status with +noall +comments when you need to tell NXDOMAIN from an empty answer.