나는 그 이해 exec
내가 할 수 있습니다 / O는 현재 쉘에,하지만 난 단지 사용을 같이보고 재 :
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
합니까?
추신 : 나는 이것을 Bats 소스 코드 에서 찾았습니다.
답변
보낸 사람 bash manpage
:
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
:
sudo strace -f -s 200 -e trace=dup2 bash redirect.sh
의 경우 3<&1
:
dup2(3, 255) = 255
dup2(1, 3) = 3
의 경우 3>&1
:
dup2(1, 3) = 3
의 경우 2>&1
:
dup2(1, 2) = 2
stdout을 파일 설명자 3에 복제 3<&1
하는 것과 정확히 동일한 것으로 보입니다 3>&1
.