Using exec 1>&2 in a POSIX shell script function. Will it interfere with other io-redirects?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am in process of writing a POSIX shell script.
I have one function, which redirects (almost) all output to standard error stream like that:
# print something to stderr
printf "..." $var 1>&2
It has been pointed out to me that there is a way to redirect all of the function's output with:
# redirect all output to standard error stream
exec 1>&2
keeping this line in the beginning of the function.
If there were no other redirects, I would simply agree probably, but I have there a test for color support:
# check if we have color support
if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
Basically, I don't know how the exec
works, and I don't need to know much in detail now, I just need to know if that exec
line will interfere with the above color support test anyhow?
scripting io-redirection posix
add a comment |Â
up vote
1
down vote
favorite
I am in process of writing a POSIX shell script.
I have one function, which redirects (almost) all output to standard error stream like that:
# print something to stderr
printf "..." $var 1>&2
It has been pointed out to me that there is a way to redirect all of the function's output with:
# redirect all output to standard error stream
exec 1>&2
keeping this line in the beginning of the function.
If there were no other redirects, I would simply agree probably, but I have there a test for color support:
# check if we have color support
if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
Basically, I don't know how the exec
works, and I don't need to know much in detail now, I just need to know if that exec
line will interfere with the above color support test anyhow?
scripting io-redirection posix
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am in process of writing a POSIX shell script.
I have one function, which redirects (almost) all output to standard error stream like that:
# print something to stderr
printf "..." $var 1>&2
It has been pointed out to me that there is a way to redirect all of the function's output with:
# redirect all output to standard error stream
exec 1>&2
keeping this line in the beginning of the function.
If there were no other redirects, I would simply agree probably, but I have there a test for color support:
# check if we have color support
if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
Basically, I don't know how the exec
works, and I don't need to know much in detail now, I just need to know if that exec
line will interfere with the above color support test anyhow?
scripting io-redirection posix
I am in process of writing a POSIX shell script.
I have one function, which redirects (almost) all output to standard error stream like that:
# print something to stderr
printf "..." $var 1>&2
It has been pointed out to me that there is a way to redirect all of the function's output with:
# redirect all output to standard error stream
exec 1>&2
keeping this line in the beginning of the function.
If there were no other redirects, I would simply agree probably, but I have there a test for color support:
# check if we have color support
if command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
Basically, I don't know how the exec
works, and I don't need to know much in detail now, I just need to know if that exec
line will interfere with the above color support test anyhow?
scripting io-redirection posix
scripting io-redirection posix
asked Oct 4 at 4:06
Vlastimil
6,8871149124
6,8871149124
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
exec <redirection>
will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.
So, with something like:
exec >/some/file 2>&1
foo >/another/file
- After the
exec
, the shell's stdout goes/some/file
, and stderr also goes to/some/file
. - When starting foo, the shell redirects foo's stdout to
/another/file
, but stderr is inherited unchanged and will go to/some/file
.
In these commands:
command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null
, then stderr as well. So that particular exec
line won't affect these commands.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
exec <redirection>
will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.
So, with something like:
exec >/some/file 2>&1
foo >/another/file
- After the
exec
, the shell's stdout goes/some/file
, and stderr also goes to/some/file
. - When starting foo, the shell redirects foo's stdout to
/another/file
, but stderr is inherited unchanged and will go to/some/file
.
In these commands:
command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null
, then stderr as well. So that particular exec
line won't affect these commands.
add a comment |Â
up vote
2
down vote
accepted
exec <redirection>
will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.
So, with something like:
exec >/some/file 2>&1
foo >/another/file
- After the
exec
, the shell's stdout goes/some/file
, and stderr also goes to/some/file
. - When starting foo, the shell redirects foo's stdout to
/another/file
, but stderr is inherited unchanged and will go to/some/file
.
In these commands:
command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null
, then stderr as well. So that particular exec
line won't affect these commands.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
exec <redirection>
will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.
So, with something like:
exec >/some/file 2>&1
foo >/another/file
- After the
exec
, the shell's stdout goes/some/file
, and stderr also goes to/some/file
. - When starting foo, the shell redirects foo's stdout to
/another/file
, but stderr is inherited unchanged and will go to/some/file
.
In these commands:
command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null
, then stderr as well. So that particular exec
line won't affect these commands.
exec <redirection>
will redirect the current shell's (or subshell's) I/O streams. And since commands started by the shell inherit the shell's I/O streams, those redirections will affect them, with command-specific redirections applied on top.
So, with something like:
exec >/some/file 2>&1
foo >/another/file
- After the
exec
, the shell's stdout goes/some/file
, and stderr also goes to/some/file
. - When starting foo, the shell redirects foo's stdout to
/another/file
, but stderr is inherited unchanged and will go to/some/file
.
In these commands:
command -v tput > /dev/null 2>&1 && tput setaf 1 > /dev/null 2>&1
You're not using the inherited stdout/stderr streams at all. First, stdout is redirected to /dev/null
, then stderr as well. So that particular exec
line won't affect these commands.
edited Oct 4 at 6:28
Vlastimil
6,8871149124
6,8871149124
answered Oct 4 at 4:34
Olorin
1,35411
1,35411
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473131%2fusing-exec-12-in-a-posix-shell-script-function-will-it-interfere-with-other-i%23new-answer', 'question_page');
);
Post as a guest
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
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
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