Shell, behavior of conflicting redirection2019 Community Moderator ElectionCalling shell commands from RubyCheck if a directory exists in a shell scriptHow to manage a redirect request after a jQuery Ajax callHow do I prompt for Yes/No/Cancel input in a Linux shell script?How do I redirect to another webpage?How do I make a redirect in PHP?In the shell, what does “ 2>&1 ” mean?How can I redirect and append both stdout and stderr to a file with Bash?How do I redirect with JavaScript?Check existence of input argument in a Bash shell script
Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)
Aluminum electrolytic or ceramic capacitors for linear regulator input and output?
Why do newer 737s use two different styles of split winglets?
Are all passive ability checks floors for active ability checks?
Describing a chess game in a novel
How to terminate ping <dest> &
What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?
How can we have a quark condensate without a quark potential?
How to make healing in an exploration game interesting
How could a scammer know the apps on my phone / iTunes account?
Why Choose Less Effective Armour Types?
Why is the President allowed to veto a cancellation of emergency powers?
About the actual radiative impact of greenhouse gas emission over time
Are ETF trackers fundamentally better than individual stocks?
What's the meaning of a knight fighting a snail in medieval book illustrations?
How do you talk to someone whose loved one is dying?
PTIJ: Who should I vote for? (21st Knesset Edition)
How to explain that I do not want to visit a country due to personal safety concern?
Why is a white electrical wire connected to 2 black wires?
What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?
If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?
What are substitutions for coconut in curry?
English sentence unclear
Math equation in non italic font
Shell, behavior of conflicting redirection
2019 Community Moderator ElectionCalling shell commands from RubyCheck if a directory exists in a shell scriptHow to manage a redirect request after a jQuery Ajax callHow do I prompt for Yes/No/Cancel input in a Linux shell script?How do I redirect to another webpage?How do I make a redirect in PHP?In the shell, what does “ 2>&1 ” mean?How can I redirect and append both stdout and stderr to a file with Bash?How do I redirect with JavaScript?Check existence of input argument in a Bash shell script
I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.
echo test >&2 2>&1
This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?
I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?
Thanks in advance.
edit: I'm running my script on bash.
shell redirect
add a comment |
I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.
echo test >&2 2>&1
This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?
I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?
Thanks in advance.
edit: I'm running my script on bash.
shell redirect
BashFAQ #55 (Tell me all about2>&1
-- what's the difference between2>&1 >foo
and>foo 2>&1
, and when do I use which?) is pertinent.
– Charles Duffy
Mar 7 at 16:27
add a comment |
I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.
echo test >&2 2>&1
This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?
I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?
Thanks in advance.
edit: I'm running my script on bash.
shell redirect
I'm facing to a problem that I'm not able to explain. I've to recode a shell, and there is a weird behavior for me.
echo test >&2 2>&1
This kind of command is writing on stderr (so with the first redirection), why the second redirection isn't impacting the output destination ? Why the output isn't on stdout ?
I saw some stuff that redirection is happening before executing command, from left to right, so why the second redirection is not cancelling the first one ?
Thanks in advance.
edit: I'm running my script on bash.
shell redirect
shell redirect
edited Mar 7 at 15:38
rSim
asked Mar 7 at 15:29
rSimrSim
298
298
BashFAQ #55 (Tell me all about2>&1
-- what's the difference between2>&1 >foo
and>foo 2>&1
, and when do I use which?) is pertinent.
– Charles Duffy
Mar 7 at 16:27
add a comment |
BashFAQ #55 (Tell me all about2>&1
-- what's the difference between2>&1 >foo
and>foo 2>&1
, and when do I use which?) is pertinent.
– Charles Duffy
Mar 7 at 16:27
BashFAQ #55 (Tell me all about
2>&1
-- what's the difference between 2>&1 >foo
and >foo 2>&1
, and when do I use which?) is pertinent.– Charles Duffy
Mar 7 at 16:27
BashFAQ #55 (Tell me all about
2>&1
-- what's the difference between 2>&1 >foo
and >foo 2>&1
, and when do I use which?) is pertinent.– Charles Duffy
Mar 7 at 16:27
add a comment |
2 Answers
2
active
oldest
votes
cmd >&2 2>&1
is very different than cmd 2>&1 >&2
, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1
will redirect the stdout of cmd
to the file named error
and then redirect stderr of cmd
to whatever fd 1 is connected to, namely the file named error
. But cmd 2>&1 >&2
will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1
writes everything to stderr, and cmd 2>&1 >&2
writes everything on stdout.
add a comment |
x>&y
means "redirect file descriptor x to whatever fd y is currently pointing to.
Redirections are processed strictly left to right.
So, >&2 2>&1
points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.
If you want to swap stderr and stdout, you need a 3rd file descriptor:
(echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
# ................................................ A .. B .. C .. D
# A. fd 3 = /dev/stdout
# B. fd 1 = /dev/stderr
# C. fd 2 = /dev/stdout
# D. fd 3 is closed
Let's put that in a function for easier testing
fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-;
Run it
$ fdtest
test to stdout
test to stderr
Throw away standard error (we expect to see the "stderr" message on standard out).
$ fdtest 2>/dev/null
test to stderr
Throw away standard out (we expect to see the "stdout" message on standard err).
$ fdtest 1>/dev/null
test to stdout
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047396%2fshell-behavior-of-conflicting-redirection%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
cmd >&2 2>&1
is very different than cmd 2>&1 >&2
, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1
will redirect the stdout of cmd
to the file named error
and then redirect stderr of cmd
to whatever fd 1 is connected to, namely the file named error
. But cmd 2>&1 >&2
will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1
writes everything to stderr, and cmd 2>&1 >&2
writes everything on stdout.
add a comment |
cmd >&2 2>&1
is very different than cmd 2>&1 >&2
, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1
will redirect the stdout of cmd
to the file named error
and then redirect stderr of cmd
to whatever fd 1 is connected to, namely the file named error
. But cmd 2>&1 >&2
will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1
writes everything to stderr, and cmd 2>&1 >&2
writes everything on stdout.
add a comment |
cmd >&2 2>&1
is very different than cmd 2>&1 >&2
, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1
will redirect the stdout of cmd
to the file named error
and then redirect stderr of cmd
to whatever fd 1 is connected to, namely the file named error
. But cmd 2>&1 >&2
will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1
writes everything to stderr, and cmd 2>&1 >&2
writes everything on stdout.
cmd >&2 2>&1
is very different than cmd 2>&1 >&2
, since the redirections occur in a different order. Suppose the command is invoked from a process which has fd 1 attached to a file named 'output' and fd 2 attached to a filed named 'error'. Then cmd >&2 2>&1
will redirect the stdout of cmd
to the file named error
and then redirect stderr of cmd
to whatever fd 1 is connected to, namely the file named error
. But cmd 2>&1 >&2
will first redirect fd 2 to 'output' and the redirect fd 1 to the same place. In other words, cmd >&2 2>&1
writes everything to stderr, and cmd 2>&1 >&2
writes everything on stdout.
answered Mar 7 at 16:25
William PursellWilliam Pursell
133k32206240
133k32206240
add a comment |
add a comment |
x>&y
means "redirect file descriptor x to whatever fd y is currently pointing to.
Redirections are processed strictly left to right.
So, >&2 2>&1
points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.
If you want to swap stderr and stdout, you need a 3rd file descriptor:
(echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
# ................................................ A .. B .. C .. D
# A. fd 3 = /dev/stdout
# B. fd 1 = /dev/stderr
# C. fd 2 = /dev/stdout
# D. fd 3 is closed
Let's put that in a function for easier testing
fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-;
Run it
$ fdtest
test to stdout
test to stderr
Throw away standard error (we expect to see the "stderr" message on standard out).
$ fdtest 2>/dev/null
test to stderr
Throw away standard out (we expect to see the "stdout" message on standard err).
$ fdtest 1>/dev/null
test to stdout
add a comment |
x>&y
means "redirect file descriptor x to whatever fd y is currently pointing to.
Redirections are processed strictly left to right.
So, >&2 2>&1
points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.
If you want to swap stderr and stdout, you need a 3rd file descriptor:
(echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
# ................................................ A .. B .. C .. D
# A. fd 3 = /dev/stdout
# B. fd 1 = /dev/stderr
# C. fd 2 = /dev/stdout
# D. fd 3 is closed
Let's put that in a function for easier testing
fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-;
Run it
$ fdtest
test to stdout
test to stderr
Throw away standard error (we expect to see the "stderr" message on standard out).
$ fdtest 2>/dev/null
test to stderr
Throw away standard out (we expect to see the "stdout" message on standard err).
$ fdtest 1>/dev/null
test to stdout
add a comment |
x>&y
means "redirect file descriptor x to whatever fd y is currently pointing to.
Redirections are processed strictly left to right.
So, >&2 2>&1
points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.
If you want to swap stderr and stdout, you need a 3rd file descriptor:
(echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
# ................................................ A .. B .. C .. D
# A. fd 3 = /dev/stdout
# B. fd 1 = /dev/stderr
# C. fd 2 = /dev/stdout
# D. fd 3 is closed
Let's put that in a function for easier testing
fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-;
Run it
$ fdtest
test to stdout
test to stderr
Throw away standard error (we expect to see the "stderr" message on standard out).
$ fdtest 2>/dev/null
test to stderr
Throw away standard out (we expect to see the "stdout" message on standard err).
$ fdtest 1>/dev/null
test to stdout
x>&y
means "redirect file descriptor x to whatever fd y is currently pointing to.
Redirections are processed strictly left to right.
So, >&2 2>&1
points fd 1 to something like /dev/stderr, and then points fd 2 to /dev/stderr also.
If you want to swap stderr and stdout, you need a 3rd file descriptor:
(echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-
# ................................................ A .. B .. C .. D
# A. fd 3 = /dev/stdout
# B. fd 1 = /dev/stderr
# C. fd 2 = /dev/stdout
# D. fd 3 is closed
Let's put that in a function for easier testing
fdtest() (echo "test to stdout"; echo "test to stderr" >&2) 3>&1 1>&2 2>&3 3>&-;
Run it
$ fdtest
test to stdout
test to stderr
Throw away standard error (we expect to see the "stderr" message on standard out).
$ fdtest 2>/dev/null
test to stderr
Throw away standard out (we expect to see the "stdout" message on standard err).
$ fdtest 1>/dev/null
test to stdout
edited Mar 7 at 16:29
answered Mar 7 at 16:24
glenn jackmanglenn jackman
170k26147240
170k26147240
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047396%2fshell-behavior-of-conflicting-redirection%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
BashFAQ #55 (Tell me all about
2>&1
-- what's the difference between2>&1 >foo
and>foo 2>&1
, and when do I use which?) is pertinent.– Charles Duffy
Mar 7 at 16:27