0

I want to set the first line of the following dig command output to a variable:

root@kali:~# dig +short stackoverflow.com 
151.101.129.69
151.101.65.69
151.101.1.69
151.101.193.69

So it outputs so many IP adresses, but I want only the first one which can be done by sed or head like shown below:

root@kali:~# dig +short stackoverflow.com | sed -n 1p
151.101.193.69

Problem:

When I set this to a variable, it gives me all IP adresses:

root@kali:~# cmd='dig +short stackoverflow.com | sed -n 1p'
root@kali:~# $cmd
151.101.129.69
151.101.65.69
151.101.1.69
151.101.193.69
l h
  • 31
  • 7

2 Answers2

5

If you want to save the output in a variable, use $(...):

$ ip=$(dig +short stackoverflow.com | sed -n 1p)
$ echo "$ip"
151.101.129.69

If you want to make a shortcut that you can run more than once, use a function:

$ cmd() { dig +short stackoverflow.com | sed -n 1p; }
$ cmd
151.101.129.69
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
  • 1
    @IvoYordanov, the demonstration of `cmd='dig +short stackoverflow.com | sed -n 1p'; $cmd` in the question makes the use of a function (as a real/working way to do the thing that was shown) relevant/topical. Maybe the OP *meant* `cmd=$(dig +short stackoverflow.com | sed -n 1p); echo "$cmd"`, but the literal reading is an attempt to assign code to a variable and execute it later. – Charles Duffy Nov 29 '18 at 19:04
  • @CharlesDuffy he said variable. He should learn what a variable is and not confuse between variable and function. I feel your answer can confuse him. If brave enough he will learn more of bash. – Ivo Yordanov Nov 29 '18 at 19:09
  • 1
    @IvoYordanov, yes, they *said* "variable", but as you yourself point out above, there's uncertainty / lack-of-clarity about whether they understood the meaning (and restrictions on what use cases variables are appropriate for) correctly in making that choice of terms. Answering for multiple reasonable interpretations of the question, *especially* when the OP demonstrates an usage pattern that implies that the literal interpretation of a term they used doesn't properly align with their intent, is worthwhile. – Charles Duffy Nov 29 '18 at 19:10
  • 1
    "My" answer? I haven't answered here... but I don't see any part of the question that arrays are responsive to, *or* any part of the question that implies that the OP may have *thought* they could get array behavior from non-array variables. The phrase "reasonable interpretations" in my comment above is important, and you distort my meaning (and thus respond to an argument I never made) by pretending it was not present. :) – Charles Duffy Nov 29 '18 at 19:20
  • Charles, I adore your pedantic reasonableness. Never change. – John Kugelman Nov 29 '18 at 19:24
  • "Sarcasm" Charles... – Ivo Yordanov Nov 29 '18 at 19:51
1

In order to assign the value you need to define it like this:

cmd=$(dig +short stackoverflow.com | sed -n 1p)

Execution:

ivo@spain-nuc-03:~/Downloads/TestStackoverflow$ echo "${cmd}"
151.101.193.69
Ivo Yordanov
  • 136
  • 1
  • 8