About those script commands “1>“ “2>“ “2>&1“

本文详细解释了Shell脚本中常见的重定向命令“1>”、“2>”和“2>&1”的用法及含义。通过实例展示了如何使用这些命令来控制标准输出和错误输出的流向,帮助读者更好地理解并应用这些命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

About those script commands “1>” “2>” “2>&1”

  • Prepared file descriptior info
    standard input file descriptor == 0
    standard output file descriptor == 1
    standard error file descriptor == 2

  • Prepared redirection operation info
    > aims to redirect the output path elsewhere instead of standard location(such as previous 1, 2); note: it would overwrite the previous output results.
    >> aims to redirect the output path elsewhere instead of standard location(such as previous 1, 2); note: it would append new results to the previous output.)

  • Some examples to keep those points in mind.
    1>loglist.txt : which represents that the succeed executive results would be stored to loglist.txt file.

    echo "hello_World!" >loglist.txt(or  echo "hello" 1>loglist.txt)
    

    2>errorInfo.log : which means the error msg of falilure to execute script cmd would be stored to errorInfo.log file.

    call :hello_World  >test.log 2>errorInfo.log
    

    (respectively warning or error info stored to errorInfo.log file if callback failed, and normal msg to test.log if successed)

    2>&1 :which represents that the error output of falilure to execute script cmd would be redirected to 1(stdout). //将标准错误输出重定向到标准输出

    where git.exe 1>test.log  2>&1 
    ...
    

    (//error info would be overwritten into test.log file if no git tool been installed in the PC)


Extension

1> nul : which represents that successful executive results would not be output(definitely the default output), however the failure to execute would be output by 2(standard error file descriptor)

2>nul : which represents that failed executive results would not be output, however the successful executive would be output by 1(standard output file descriptor)


ok, those words just marking what I’ve got in store for future application.In the future, maybe somewhere I really need those prep points.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值