跳转到主要内容
eLearner.app
模块 3 · 第 1 课(共 2)课程中的6/11~15 min
模块课程(1/2)

输出和重定向(echo、cat、>)

In Linux, almost all commands read data from an input stream and write results to an output stream. These channels are called standard streams:

  • Standard Input (stdin): The stream from which the command reads its input (default is the keyboard).
  • Standard Output (stdout): The stream to which the command writes its successful output (default is the terminal screen).
  • Standard Error (stderr): The stream to which the command writes its error messages (default is the screen).

Using the command line, we can redirect these streams to save results directly into files.


Printing Text: echo

The echo command simply writes its arguments to its standard output (the screen):

Bash
echo "Hello everyone"

Redirecting Output (Overwrite): >

If you want to save the output of a command to a file instead of displaying it on the screen, you can use the greater-than redirect operator >:

Bash
echo "Hello everyone" > greetings.txt

This command runs echo and redirects its output to the file greetings.txt.

[!IMPORTANT] If the file greetings.txt does not exist, it is created. If it already exists, its existing content is completely deleted and overwritten with the new output.


Reading File Contents: cat

To view the content of one or more text files on the fly, use the cat command (short for concatenate):

Bash
cat greetings.txt

Appending Output: >>

If you want to append new lines to the end of an existing file rather than overwriting it, you must use the append operator >>:

Bash
echo "New line of text" >> greetings.txt

Now, reading the file with cat greetings.txt will show both the first line and the second line.


Usage Examples and Common Errors

You can pass multiple files to cat at the same time to display them sequentially (concatenated):

Bash
cat notes.txt info.txt
# Output:
# Keep learning Linux!
# Linux is awesome.

You can redirect the combined output to a third file using the > operator:

Bash
cat notes.txt info.txt > combined.txt

If you try to read a file that does not exist, cat will write an error message:

Bash
cat nonexistent_file.txt
# Output:
# cat: nonexistent_file.txt: No such file or directory

Try it yourself

Exercise 1: Create a file with a greeting

锻炼#linux.m3.l1.e1
尝试:0加载中...

Create a file named 'hello.txt' containing exactly the text 'Hello World' using echo and the > operator.

正在加载编辑器...
显示提示

Use echo followed by the string in quotes, the > operator, and hello.txt.

3 次尝试后可用的解决方案

Exercise 2: Append notes to a file

锻炼#linux.m3.l1.e2
尝试:0加载中...

Append the line 'Linux is fun' to the end of the existing 'notes.txt' file without deleting its current contents.

正在加载编辑器...
显示提示

Use the >> operator to append text to the notes.txt file.

3 次尝试后可用的解决方案

Exercise 3: Concatenate files

锻炼#linux.m3.l1.e3
尝试:0加载中...

Concatenate the contents of the files 'notes.txt' and 'info.txt' and save the combined output to a new file named 'combined.txt'.

正在加载编辑器...
显示提示

Use cat followed by the names of the two source files and redirect the output with > to the destination file.

3 次尝试后可用的解决方案