1

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
  • 41
  • 1
  • 8
  • 1
    No need for `sed` or `head` or anything else. `read -r cmd < <(dig +short stackoverflow.com)` – Charles Duffy Nov 29 '18 at 18:54
  • @CharlesDuffy's comment gives me the following error: `-sh: syntax error near unexpected token '<'` – Stunner Mar 30 '23 at 16:23
  • @Stunner, This question is tagged bash, so it's appropriate for comments and answers to use bash-only syntax. `sh` is not `bash`: even on operating systems where it's a symlink to bash, bash turns off some features for better compatibility when started under the `sh` name. If you want to use bash features, start the shell with the command `bash`, not `sh`. If you want compatibility with POSIX sh, stick to Stack Overflow questions tagged `sh`, not ones tagged `bash`. – Charles Duffy Mar 30 '23 at 16:23
  • Question has `bash` tag so I thought this would work in bash. Do you happen to know of a bash equivalent to your command? – Stunner Mar 30 '23 at 16:26
  • 1
    @Stunner, your error message says you aren't _trying_ to make it work in bash, you're trying to make it work in sh. sh is not bash. – Charles Duffy Mar 30 '23 at 16:26
  • When you get an error from bash it says `bash: ...things here...`, not `sh: ...things here...` – Charles Duffy Mar 30 '23 at 16:27
  • @Stunner, did you maybe start your script with `sh yourscript`? Change it to `bash yourscript`. Or if it starts with `#!/bin/sh`, change it to `#!/usr/bin/env bash`. – Charles Duffy Mar 30 '23 at 16:28
  • Synology NASes unfortunately default to `sh` apparently. `bash` can be executed but I still don't get the result I seek. – Stunner Mar 30 '23 at 16:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252881/discussion-between-stunner-and-charles-duffy). – Stunner Mar 30 '23 at 18:17

2 Answers2

6

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
  • 349,597
  • 67
  • 533
  • 578
  • 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
  • 146
  • 1
  • 8