Sobes.tech
Senior

What is the difference between a single angle bracket (>) and double angle brackets (>>) when writing to a file?

sobes.tech AI

Answer from AI

In the context of writing to a file (for example, in a Linux shell), a single angle bracket > and two angle brackets >> serve different functions:

  • > — redirects the output of a command to a file, overwriting the file's contents. If the file exists, its contents will be deleted and replaced with new data.

  • >> — redirects the output of a command to a file, appending data to the end of the file. If the file exists, new data will be added after the current contents.

Example:

# Overwrite the file output.txt
echo "Hello" > output.txt

# Append a line to the end of output.txt
echo "World" >> output.txt

After executing these commands, the file output.txt will contain:

Hello
World

Thus, > is used for creating or replacing a file, while >> is used for adding to an existing file.

What is the difference between a single angle bracket… - sobes.tech