首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >执行3<&1是做什么的?

执行3<&1是做什么的?
EN

Unix & Linux用户
提问于 2014-03-20 06:55:03
回答 1查看 28.4K关注 0票数 16

我知道exec可以在当前shell上进行I/O重定向,但我只看到以下用法:

代码语言:javascript
复制
exec 6<&0   # Link file descriptor #6 with stdin.
            # Saves stdin.

exec 6>&1   # Link file descriptor #6 with stdout.
            # Saves stdout.

由此我了解到,<用于输入流,>用于输出流。那么exec 3<&1是做什么的呢?

PS:我从蝙蝠源代码找到这个

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2014-03-20 07:00:53

来自bash manpage

代码语言:javascript
复制
Duplicating File Descriptors
       The redirection operator

              [n]<&word

       is used to duplicate input file descriptors.  If word expands to one or
       more  digits,  the file descriptor denoted by n is made to be a copy of
       that file descriptor.  If the digits in word  do  not  specify  a  file
       descriptor  open for input, a redirection error occurs.  If word evalu‐
       ates to -, file descriptor n is closed.  If n  is  not  specified,  the
       standard input (file descriptor 0) is used.

       The operator

              [n]>&word

       is  used  similarly  to duplicate output file descriptors.  If n is not
       specified, the standard output (file descriptor 1)  is  used.   If  the
       digits  in word do not specify a file descriptor open for output, a re‐
       direction error occurs.  As a special case, if n is omitted,  and  word
       does not expand to one or more digits, the standard output and standard
       error are redirected as described previously.

我用strace做了一些调试:

代码语言:javascript
复制
sudo strace -f -s 200 -e trace=dup2 bash redirect.sh

对于3<&1

代码语言:javascript
复制
dup2(3, 255)                            = 255
dup2(1, 3)                              = 3

对于3>&1

代码语言:javascript
复制
dup2(1, 3)                              = 3

对于2>&1

代码语言:javascript
复制
dup2(1, 2)                              = 2

3<&1似乎与3>&1完全一样,将stdout复制到文件描述符3。

票数 16
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/120532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档