-   Linux - Newbie (
-   -   BASH Script. Result of command input into variable (

bcyork 12-05-2011 09:40 PM

BASH Script. Result of command input into variable
Hi, I have been trying to figure this out and also new to bash scripting as well. How can I put the result of a command into a variable such as this.

CURRENT='wget -O - -q'

So if I would run


I would get an IP address?

Right now echo returns the command. I can't seem to figure out how exactly to do this. In the end I need two variables. One an IP address returned as my current external IP and the second a variable read from a file that would be an IP address as well.


corp769 12-05-2011 09:43 PM

Here is what you need:

var1=$(wget -O - -q
echo $var1

You need to tell it to execute the command, instead of trying to display it. You can alternatively use backticks, as such:

`wget -O - -q`


bcyork 12-05-2011 09:58 PM

Thanks Josh, did the trick!


corp769 12-05-2011 11:45 PM

No problem! Please mark your thread as solved using the thread tools located at the top of the page, thanks!



David the H. 12-06-2011 01:57 PM

Note that $(..) is highly recommended over `..`.

A while back I wrote up a script/function that polls a random site for your external ip address, and keeps trying until it gets a successful hit. You can easily edit the list if you discover new ones or one goes bad. I currently have seven working addresses.



get_current_ip() {
        local -a ipsite
        local -i xc num i



        until (( xc == 0 )) ; do
                (( i = RANDOM % num ))
                ip=$( wget -t 1 -T 5 -q -O- "${ipsite[i]}" )

        #some of the sites may output a newline or other junk, so remove any cruft.
        printf "%s" "${ip//[^0-9.]}"


exit 0

All times are GMT -5. The time now is 10:13 AM.