请教下 Linux 的输入输出 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
mingtdlb
V2EX    Linux

请教下 Linux 的输入输出

 
  •   mingtdlb 2021-03-21 00:26:30 +08:00 2240 次点击
    这是一个创建于 1748 天前的主题,其中的信息可能已经有所发展或是发生改变。

    &> file 、>& file 、>file 2>&1,这三个有啥区别?

    4 条回复    2021-03-21 23:48:53 +08:00
    yinusxxxx
        1
    yinusxxxx  
       2021-03-21 00:43:55 +08:00   1
    Redirecting Standard Output and Standard Error
    This construct allows both the standard output (file descriptor 1) and
    the standard error output (file descriptor 2) to be redirected to the
    file whose name is the expansion of word.

    There are two formats for redirecting standard output and standard
    error:

    &>word
    and
    >&word

    Of the two forms, the first is preferred. This is semantically equiva-
    lent to

    >word 2>&1

    When using the second form, word may not expand to a number or -. If
    it does, other redirection operators apply (see Duplicating File
    Descriptors below) for compatibility reasons.


    来自 man bash,综上所述,这三个没有功能上的区别,写法不同而已
    Jat001
        2
    Jat001  
       2021-03-21 00:44:37 +08:00
    &> file 跟 >file 2>&1 一样,都是把标准输出 stdout 和错误输出 stderr 写到 file 里,1 代表标准输出,2 代表错误输出
    2>&1 的意思把 stderr 写入到 stdout 里,>file 在前还是再后无所谓,最后都是写到文件里

    >& file 还真没见过
    shakoon
        3
    shakoon  
       2021-03-21 08:34:43 +08:00 via Android
    1 的错误输出在 console,不会写入 file,其他输出写入 file
    2 没见过这种写法,不知道
    3 错误输出到 file,console 什么也不会输出,适合完整记录日志
    楼上两位说的不对,至少 1 和 3 是有明显区别的
    no1xsyzy
        4
    no1xsyzy  
       2021-03-21 23:48:53 +08:00
    @shakoon
    (echo out; echo err >&2 ) &> temp1.txt
    (echo out; echo err >&2 ) >& temp2.txt
    (echo out; echo err >&2 ) > temp3.txt 2>&1
    看看结果?

    @mingtdlb 这不是 Linux 的任何方面,这是 Bash 语法
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     973 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 18:06 PVG 02:06 LAX 10:06 JFK 13:06
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86