Shell Script: How to write a string to file and to stdout on console?

BashShellCommand LineShKsh

Bash Problem Overview


How to write a string to file and to stdout on console?

If I do

echo "hello" > logfile.txt

I view only hello in logfile.txt but how can I write hello also on console of Linux?

Bash Solutions


Solution 1 - Bash

Use the tee command:

echo "hello" | tee logfile.txt

Solution 2 - Bash

You can use >> to print in another file.

echo "hello" >> logfile.txt

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
QuestionCatanzaroView Question on Stackoverflow
Solution 1 - BashBiffenView Answer on Stackoverflow
Solution 2 - BashSuperNovaView Answer on Stackoverflow