← Lessons/Variables & Output
← Variables & Output

Redirect to a file

Learn

Output into a file: redirection

echo writes to standard output, the stream your terminal displays. That output doesn't have to go to the screen: > redirects it into a file.

echo "logged"  > out.txt   # writes "logged" to out.txt (creating or overwriting it)
echo "another" >> out.txt  # >> APPENDS instead of overwriting

> is a funnel: it catches what a command would have printed and pours it into a file instead. The trap: a single > overwrites the file from scratch every time; use >> when you mean to add to the end.

The editor opens with your script from the previous step: add to it.

Now also write that same greeting into a file called greeting.txt using >.

Interview prep

Bash quoting is a rite of passage.

Why double quotes around "$name" instead of single quotes?

Double quotes expand variables (and command substitution) while still protecting against word-splitting on spaces. Single quotes are fully literal — '$name' prints the text $name.

$ bash solution.sh
Loading...

Output

Run your solution to see its output here.