How to concatenate stdin and a string?

Bash

Bash Problem Overview


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

echo "input" | COMMAND "string"

and get

inputstring

Bash Solutions


Solution 1 - Bash

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"

Solution 2 - Bash

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"

Solution 3 - Bash

I'm often using pipes, so this tends to be an easy way to prefix and suffix stdin:

echo -n "my standard in" | cat <(echo -n "prefix... ") - <(echo " ...suffix")
prefix... my standard in ...suffix

Solution 4 - Bash

There are some ways of accomplish this, i personally think the best is:

echo input | while read line; do echo $line string; done

Another can be by substituting "$" (end of line character) with "string" in a sed command:

echo input | sed "s/$/ string/g"

Why i prefer the former? Because it concatenates a string to stdin instantly, for example with the following command:

(echo input_one ;sleep 5; echo input_two ) | while read line; do echo $line string; done

you get immediatly the first output:

input_one string

and then after 5 seconds you get the other echo:

input_two string

On the other hand using "sed" first it performs all the content of the parenthesis and then it gives it to "sed", so the command

(echo input_one ;sleep 5; echo input_two ) | sed "s/$/ string/g"

will output both the lines

input_one string
input_two string

after 5 seconds.

This can be very useful in cases you are performing calls to functions which takes a long time to complete and want to be continuously updated about the output of the function.

Solution 5 - Bash

You can do it with sed:

seq 5 | sed '$a\6'
seq 5 | sed '$ s/.*/\0 6/'

In your example:

echo input | sed 's/.*/\0string/'

Solution 6 - Bash

I know this is a few years late, but you can accomplish this with the xargs -J option:

echo "input" | xargs -J "%" echo "%" "string"

And since it is xargs, you can do this on multiple lines of a file at once. If the file 'names' has three lines, like:

Adam
Bob
Charlie

You could do:

cat names | xargs -n 1 -J "%" echo "I like" "%" "because he is nice"

Solution 7 - Bash

The command you posted would take the string "input" use it as COMMAND's stdin stream, which would not produce the results you are looking for unless COMMAND first printed out the contents of its stdin and then printed out its command line arguments.

It seems like what you want to do is more close to command substitution.

http://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html#Command-Substitution

With command substitution you can have a commandline like this:

echo input `COMMAND "string"`

This will first evaluate COMMAND with "string" as input, and then expand the results of that commands execution onto a line, replacing what's between the ‘`’ characters.

Solution 8 - Bash

Also works:

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

Will generate strings like:

string 000
string 001
string 002
string 003
string 004

...

Solution 9 - Bash

cat will be my choice: ls | cat - <(echo new line)

Solution 10 - Bash

With perl

echo "input" | perl -ne 'print "prefix $_"' 

Output:

prefix input

Solution 11 - Bash

I want to prepend my sql script with "set" statement before running it. So I echo the "set" instruction, then pipe it to cat. Command cat takes two parameters : STDIN marked as "-" and my sql file, cat joins both of them to one output. Next I pass the result to mysql command to run it as a script.

echo "set @ZERO_PRODUCTS_DISPLAY='$ZERO_PRODUCTS_DISPLAY';" | cat - sql/test_parameter.sql | mysql

p.s. mysql login and password stored in .my.cnf file

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNoam RossView Question on Stackoverflow
Solution 1 - Bashsampson-chenView Answer on Stackoverflow
Solution 2 - Bashglenn jackmanView Answer on Stackoverflow
Solution 3 - Bashrupert160View Answer on Stackoverflow
Solution 4 - BashSimoneView Answer on Stackoverflow
Solution 5 - BashVytenis BivainisView Answer on Stackoverflow
Solution 6 - Bashuser2635263View Answer on Stackoverflow
Solution 7 - BashMax DeLisoView Answer on Stackoverflow
Solution 8 - BashDmitryView Answer on Stackoverflow
Solution 9 - BashclarkttfuView Answer on Stackoverflow
Solution 10 - BashAx_View Answer on Stackoverflow
Solution 11 - BashkukulisView Answer on Stackoverflow