Linux:如何同时使用文件作为输入和输出?

我刚刚在bash中运行以下代码:
uniq .bash_history > .bash_history

我的历史档案最终完全空了.

我想在写入之前我需要一种方法来读取整个文件.
怎么做的?

PS:我显然想过使用临时文件,但我正在寻找更优雅的解决方案.

解决方法

我建议使用 moreutils的海绵.从联机帮助页:
DESCRIPTION
  sponge  reads  standard  input  and writes it out to the specified file. Unlike
  a shell redirect,sponge soaks up all its input before opening the output file.
  This allows for constructing pipelines that read from and write to the same 
  file.

要将此问题应用于您的问题,请尝试:

uniq .bash_history | sponge .bash_history

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...