bash: Redirect IO , but not with suffixes

generally I make use of XX 2>error.log, but also for debug objectives, I favor the adhering to syntax:

function XX()
{
    redirect_io_to "error.log"
}

Is there such syntax readily available?

It is hassle-free due to the fact that I arrangement my VIM to comment / uncomment a line with some faster way key, I can conveniently comment this out / uncomment it.

1
2022-06-07 14:35:07
Source Share
Answers: 2

Nope, yet you can constantly pile every little thing you require in one block:

function XX() {
 (
  some
  commands
  here
 ) 2>error.log
}
0
2022-06-07 14:57:36
Source

Yes, yet its not actually rather.

You can do exec 2> error.log to send stderr to a documents

The trouble is that you currently need to cleanse it up and also recover stderr, so at the end of the function you need to do exec 2>&1, that makes stderr return to the incurable once more.

function XX()
{
    exec 2> error.log
    foo bar
    exec 2>&1
}

The exec 2>&1 does not injured to leave in, so you can simply comment and also uncomment the first exec.


Conversely you can make the entire function execute in a subshell to make sure that redirections are tidied up instantly (the parenthesis in the listed below instance start a subshell).

function XX()
{ (
    exec 2> error.log
    foo bar
) }

Lastly, you can simply create an alias definition that you can comment and also uncomment conveniently, and also simply place it right prior to the function declaration.

alias XX='XX 2> error.log'
function XX()
{
    foo bar
}
4
2022-06-07 14:55:11
Source