66

I don't know much about bash.

My instructor asked me to make a cat script and to observe the output and then tell what is the operator > and what is the difference between the operators > & >>.

I am unable to find any justifications. Could you help?

1
  • You could fiddle with temporary files to get the difference between >> and >.
    – halp
    Nov 1, 2010 at 2:18

2 Answers 2

103

The > sign is used for redirecting the output of a program to something other than stdout (standard output, which is the terminal by default).

The >> appends to a file or creates the file if it doesn't exist.
The > overwrites the file if it exists or creates it if it doesn't exist.

In either case, the output of the program is stored in the file whose name is provided after the redirection operator.

Examples:
$ ls > allmyfiles.txt creates the file "allmyfiles.txt" and fills it with the directory listing from the ls command

$ echo "End of directory listing" >> allmyfiles.txt adds "End of directory listing" to the end of the file "allmyfiles.txt"

$ > newzerobytefile creates a new zero byte file with the name "newzerobytefile" or overwrites an existing file of the same name (making it zero bytes in size)

2
-1

=> th > operator use to overwrite the file if exist other wise it will create new file E.X. cat >example1 => If file 'example1' is exist than it will over write else create new file => the >> operator use to append the end of the file E.X. cat >>example1

1
  • 5
    That is exactly the same information as andyh_ky posted previously. There is no point in repeating a previous answer. There is also no point in adding a meaningless comment. Oct 16, 2012 at 6:32

Not the answer you're looking for? Browse other questions tagged .