Klausi
Klausi

Reputation: 11

Bash: how to do string replacements in function?

I wrote a bash script with a curl statement and many sed statements after that. I would like to move all the sed replacements in a fucntion so that the curl statement looks better. How can I pass a parameter? Should I store the result of curl in a variable? And how does return in function work?

function do_replacements () {

}

curl -s http.... | do_replacements > target.txt

Upvotes: 0

Views: 45

Answers (1)

Freddy
Freddy

Reputation: 4688

You could ignore stderr to prevent printing of the transfer summary and read from stdin in the function:

do_replacements() {
  sed 's/foo/bar/g;s/abc/def/g'
}

curl http://… 2>/dev/null | do_replacements > target.txt

There is no "return" value as such, the function prints the modified result to stdout and this output is then redirected to target.txt.

Upvotes: 1

Related Questions