🔍 See Original Answer on Ask Ubuntu 🔗
echo "hello" >&0 | > file.txt doesn't write to file.txt
March 29, 2018
Edited: March 29, 2018
November 24, 2022
Copy to clipboard: false
>symbol is for file redirection to start of a file.
>>symbol is for file redirection to the end of a file (append).
|symbol is for “piping” or passing the output from one command as input to another command.
$ echo "Hello" > file.txt $ echo "World!" >> file.txt $ cat file.txt Hello World! $ cat file.txt | grep ! World!
grep (Global regular expression print) command searches a file for a given search string and prints the line it is on.
&>0 according to Advanced-Bash Scripting Guide:
&>filename # Redirect both stdout and stderr to file "filename." # This operator is now functional, as of Bash 4, final release.
In your case the file name was 0 which is standard input. So it is redirecting all output to input. Which as best as I can tell is a circular reference that will not work. The syntax you used is
>&0 which redirects output to input. Which is also appears to be a circular reference.
The “filenames” are:
2standard error output
The traditional way of using file descriptor
0 (standard input) through file redirection is:
0< FILENAME < FILENAME # Accept input from a file. # Companion command to ">", and often used in combination with it. # # grep search-word <filename
If you want to use the
echo command and
| together (as Zanna’s answer points out) you can use:
$ echo "hello" | cat > file.txt $ cat file.txt hello