Linux清除命令中的奇怪内容:

问题描述 投票:1回答:3

今天,我只是在试验Linux shell。这是我所做的。1.我将clear的输出写到文件中:

$clear > clear.txt

2。现在,我在外壳上有一些内容(非空)。然后我尝试cat clear.txt

的内容
$cat clear.txt

令我惊讶的是,整个屏幕都被清除了。我能解释一下为什么吗?如果这是真的,为什么我们不能对所有命令都这样做?

linux bash shell ubuntu unix
3个回答
1
投票

不是一个真正的解释,但它应该如何工作:

http://man7.org/linux/man-pages/man1/clear.1.html

   @CLEAR@ writes to the standard output.  You can redirect the standard
   output to a file (which prevents @CLEAR@ from actually clearing the
   screen), and later cat the file to the screen, clearing it at that
   point.

1
投票

这是clear命令的直观表示。编辑文本文件将产生:

^[[3J^[[H^[[2J

来源:https://unix.stackexchange.com/questions/400142/terminal-h2j-caret-square-bracket-h-caret-square-bracket-2-j


0
投票

clear的工作原理非常简单,只需传输一系列由您的终端解释的控制代码即可。响应接收到这些控制代码,由终端(或终端仿真器,控制台界面或您碰巧使用的任何东西)处理真正清除屏幕的魔力。

例如,另请参见https://en.wikipedia.org/wiki/ANSI_escape_code

© www.soinside.com 2019 - 2024. All rights reserved.