Nicolas Marshall
Nicolas Marshall

Reputation: 4444

Bash : getting webpage content into variable with curl / wget

When I execute these lines :

res1=$(curl -sSL http://ipecho.net/plain | xargs echo)
echo $res1

I get this output, meaning I'm actually able to put the contents of this webpage into a variable :

92.12.242.124

========================

Now, when I try the same with another URL :

res2=$(curl -sSL https://raw.github.com/n-marshall/system-setup/master/common/configs/.gitignore_global | xargs echo)
echo $res2

I get this output :

xargs: unterminated quote

Could anyone guide me into getting the content of the second link into a variable ? Thank you.

(My actual goal is to then copy it / append it to a local file, if that changes anything to the answer)

==================================

EDIT : I haven't been able to achieve the same with wget either, but any solution with wget is welcome too

EDIT2 : to provide some more context...

I'm looking for a solution that could :

like this one (which appends but adds a separator) :

append() {
    if [ "$1" = "--separate" ]; then
        [[ -s $3 ]] && printf "\n#----------------------------------------------------------------\n\n" | sudo tee -a $3
        echo $2 | sudo tee -a $3
        printf "\n" | sudo tee -a $3
    else
        echo $1 | sudo tee -a $2
    fi
}

Upvotes: 0

Views: 1450

Answers (1)

SLePort
SLePort

Reputation: 15461

You don't need a variable if you just want to copy curl output to a local file. Just redirect the curl command to your file:

curl -sSL https://raw.github.com/n-marshall/system-setup/master/common/configs/.gitignore_global > file

To append the output to an existing file:

curl -sSL https://raw.github.com/n-marshall/system-setup/master/common/configs/.gitignore_global >> file

Edit:

With sudo:

sudo bash -c 'curl -sSL https://raw.github.com/n-marshall/system-setup/master/common/configs/.gitignore_global > file2'

Upvotes: 1

Related Questions