ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Have: a bash variable, a character string such as ...
Code:
NewRow='NEWROW'
Want: to attach the string as a new row at the top of the matrix.
This code ...
Code:
echo; echo; echo "Add a row to the top of a matrix."
echo "Input ..."; cat $InFile
echo; echo "Result ..."
NewRow='NEWROW'
cat <<< $NewRow \
|cat - $InFile
... produces this result ...
Code:
Add a row to the top of a matrix.
Input ...
abcde
fghij
klmno
pqrst
Result ...
NEWROW
abcde
fghij
klmno
pqrst
... as expected. All good.
Now, a variation on the theme.....
Have: the same matrix and the same character string.
This code (which appears to be the inverse of that shown above) ...
Code:
echo "Add a row to the bottom of a matrix."
echo "Input ..."; cat $InFile
echo; echo "Result ..."
NewRow='NEWROW'
cat $InFile \
|cat - <<< $NewRow
... produces this unexpected result ...
Code:
Add a row to the bottom of a matrix.
Input ...
abcde
fghij
klmno
pqrst
Result ...
NEWROW
Please explain.
Note 1) There are other ways to "tack on" a row to the top or bottom of a matrix, such as sed.
I'm not looking for alternate solutions, but only an explanation for why cat failed.
Note 2) Please don't chide me for a "useless" cat. That's not the point of the question.
Its late so have not thought this all the way through, however, the issue is not cat but rather your use of re-directions and which order they are being processed in.
I'll have another look in the morning in case no one else elaborates or you don't find the solution using my information
Indeed, as ntubski explained, the problem is the double redirection of the standard input from the pipe and from the here string. The reason is that the last one supersedes the preceding one. In this case the cat command doesn't receive input from the pipe anymore, but only from the here string.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.