Search code examples
bash

Format stdin in bash


I have a multi-line string coming from another program that I want to convert to a SQL command. I was hoping that printf could help me, but it doesn't seem to work:

echo -e '1\n2\n3'|printf 'SELECT %s INTO MyTable'

I was hoping to see:

SELECT '1
2
3' INTO MyTable

But I got:

SELECT  INTO MyTable

How can I get the %s to read stdin?


Solution

  • Use xargs to transform stdin to program arguments:

    echo -n  -e '1\n2\n3' |xargs -0  printf 'SELECT %s INTO MyTable'