Search code examples
bashwhile-looppipegnome

How to pass output as arguments to another command?


I am using a command,

gsettings2 monitor org.gnome.desktop.background picture-uri| cut -f2 -d "'"

This correctly gives uri for changed wallpapers. I want to pipe every such value to a function foo such that

function foo {
    echo "Value changed $1"
}

executes. How do I do it?


Solution

  • gsettings2 ... | stdbuf -oL cut -f2 -d "'" | while read -r uri; do
        foo "$uri"
    done
    

    The while read loop calls foo for each URI it reads. The stdbuf -oL call is there to force cut to be line-buffered so its output is visible immediately.