echo, > and >>
Write with echo & redirection
Lesson 8 of 20 · 8 min · Module 3: Text superpowers
Commands print text to the screen. Redirection sends that text into a file instead.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Print text with echo
- Overwrite a file with >
- Append to a file with >>
The idea
echo "text" prints text. > file sends a command’s output into a file and replaces what was there. >> file appends to the end. This works with any command: ls > files.txt saves a listing.
echo "first line" > todo.txt
echo "second line" >> todo.txt
cat todo.txt
01One arrow replaces
> creates the file if needed and replaces its whole content. Running the same > command twice leaves only the last line.
echo "one" > note.txt
echo "two" > note.txt
cat note.txttwo02Two arrows add
>> keeps what is already there and adds the new output at the end. It is how scripts build up logs and reports line by line.
03Any output can be redirected
Redirection belongs to the shell, not to echo. ls > files.txt or date >> history.txt work the same way. The command prints nothing to the screen because its output went into the file.
Your turn
todo.txt in your home folder containing exactly these three lines, in order: Install updates, Check backups, Read the logs. Use > for the first line and >> for the others.What the checks look for
- todo.txt has the three lines in order
- Uses >> to append
Take this with you
> overwrites, >> appends, and both work with every command.
Go a little further
Save the output of ls -l into a file called listing.txt and display it with cat.