shell - 展示 bash shell stdout/stderr 重定向行为的备忘单

标签 shell stdout pipe stderr tee

是否有一个很好的备忘单展示了 BASH shell 重定向的许多用途?我很想把这样的东西给我的学生。我想看到的一些例子:

cmd > output_file.txt       #redirect stdout to output_file.txt
cmd 2> output_file.txt      #redirect stderr to output_file.txt
cmd >& outpout_file.txt     #redirect both stderr and stdout to output_file.txt
cmd1      | cmd2            #pipe cmd1 stdout to cmd2's stdin
cmd1 2>&1 | cmd2            #pipe cmd1 stdout and stderr to cmd2's stdin
cmd1      | tee result.txt  #print cmd1's stdout to screen and also write to result.txt
cmd1 2>&1 | tee result.txt  #print stdout,stderr to screen while writing to result.txt

(或者我们可以将其设为社区维基并在此处列举此类内容)

谢谢!

设置Jmp

最佳答案

关于shell - 展示 bash shell stdout/stderr 重定向行为的备忘单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2604141/

相关文章:

bash - genisoimage 脚本不能刻录大于 4GB 但小于 4.5GB 的文件?

shell - 如何确保每个主机只执行一次 Ansible shell 命令?

linux - Bash - 将函数值设置为变量

python - 如何读取Python中正在进行的进程的stdout输出?

objective-c - 如何将 STDOUT 重定向到 NSTextView?

c - 在 C 中从管道写入和读取整数

c - 向管道写入一些随机数?

macos - 在 Bash 脚本中正确回显变量

python子进程隐藏stdout并等待它完成

shell - 如何在 shell 脚本中通过管道传输后台进程