How to concatenate stdin and a string?

前端 未结 9 1447
死守一世寂寞
死守一世寂寞 2021-01-30 01:49

How to I concatenate stdin to a string, like this?

echo \"input\" | COMMAND \"string\"

and get

inputstring
相关标签:
9条回答
  • 2021-01-30 02:42

    Also works:

    seq -w 0 100 | xargs -I {} echo "string "{}
    

    Will generate strings like:

    string 000
    string 001
    string 002
    string 003
    string 004
    

    ...

    0 讨论(0)
  • 2021-01-30 02:43

    A bit hacky, but this might be the shortest way to do what you asked in the question (use a pipe to accept stdout from echo "input" as stdin to another process / command:

    echo "input" | awk '{print $1"string"}'
    

    Output:

    inputstring
    

    What task are you exactly trying to accomplish? More context can get you more direction on a better solution.

    Update - responding to comment:

    @NoamRoss

    The more idiomatic way of doing what you want is then:

    echo 'http://dx.doi.org/'"$(pbpaste)"
    

    The $(...) syntax is called command substitution. In short, it executes the commands enclosed in a new subshell, and substitutes the its stdout output to where the $(...) was invoked in the parent shell. So you would get, in effect:

    echo 'http://dx.doi.org/'"rsif.2012.0125"
    
    0 讨论(0)
  • 2021-01-30 02:48

    use cat - to read from stdin, and put it in $() to throw away the trailing newline

    echo input | COMMAND "$(cat -)string"
    

    However why don't you drop the pipe and grab the output of the left side in a command substitution:

    COMMAND "$(echo input)string"
    
    0 讨论(0)
提交回复
热议问题