1

Relevant Context:

Redirect standard out

command > file
command 1> file

Redirect standard error

command 2> file

Redirect standard out and standard error to separate files

command 2> error.txt 1> output.txt

Redirect standard out and standard error to the same file

command > file 2>&1
command &> file # bash only? 
# For all I know, all the examples are bash-only, but the article called it out explicitly on this line

Warning: the following example redirects only stdout to file. This happens because the stderr is redirected to stdout before the stdout was redirected to file

command 2>&1 > file # incorrect! 

Question:

In my mind, these symbols are completely arbitrary, and therefore, difficult to memorize. I know how to alias commands, but is there some way to alias these symbols to make them easier to remember? Something like this:

Redirect standard out and standard error to the same file

command redirectStandardOutputAndError file

or even better:

command >outerr file

Bonus points if this works on zsh and bash, but I'm bash is the only requirement.

1 Answer 1

2

You can't use aliases to do this in bash, they just are not powerful enough. However you can use functions, which might help you.

This approach does require you to think about redirection a little differently. You state the redirection first, and then the command, so if you want to save the output of ls -l in /tmp/dir_listing you would type save_in /tmp/dir_listing ls -l

save_in(){ # save stdout in file
    "${@:2}" > "$1"
}

save_all(){ # save both stdout and stderr in file
     "${@:2}" > "$1" 2>&1
}

save_each(){ # save stdout and stderr in different files
     "${@:3}" > "$1" 2>"$2"
}

save_base(){ # save stdout and stderr into different file, just give base
    "${@:2}" > "$1.out" 2>"$1.err"
}

The difficulty is coming up with reasonable names that are memorable.

2
  • 1
    Re "not powerful enough": zsh does have a feature called global aliases which have this funtionality: alias -g OUTERR='&>'; ls -l OUTERR file works.
    – corvus_192
    Sep 25 at 8:42
  • @corvus_192 ooh, I may use that. I assumed bash's &> symbol would be different in zsh. Sep 25 at 18:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .