The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

If you can read me, I'm broken!

Views: 19,348β€…    Votes:  2β€…
Tags: command-line   bash  
Link: πŸ” See Original Answer on Ask Ubuntu ⧉ πŸ”—

URL: https://askubuntu.com/q/1020127
Title: echo "hello" >&0 | > file.txt doesn't write to file.txt
ID: /2018/03/29/echo-_hello_-__0-_-_-file.txt-doesn_t-write-to-file.txt
Created: March 29, 2018    Edited:  March 29, 2018
Upload: April 8, 2024    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


    $ echo "Hello" > file.txt
    $ echo "World!" >> file.txt
    $ cat file.txt
    Hello
    World!
    $ cat file.txt | grep !
    World!

The grep (Global regular expression print) command searches a file for a given search string and prints the line it is on.

The &>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:

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
⇧ Are there file-naming conventions for .cron and .systemd extensions? Ubuntu 16.04 wont boot after changing xorg.conf files  β‡©