Last active
February 13, 2023 03:32
-
-
Save MatrixManAtYrService/caa8945d26ddd7ff0c3cf2b5a4973218 to your computer and use it in GitHub Desktop.
zsh vs bash, two redirection strategies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NONSWAPPED_OUT | |
nonswapped_err | |
tee out: NONSWAPPED_OUT | |
tee err: nonswapped_err | |
swapped_err | |
SWAPPED_OUT | |
tee out: SWAPPED_OUT | |
tee err: swapped_err |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env zsh | |
#! /usr/bin/env bash | |
# switching the above lines causes different results | |
# writes to both | |
the_cmd() | |
{ | |
echo ${1}_out; | |
1>&2 echo ${1}_err; | |
} | |
# to visually distinguish stdout | |
to_upper() | |
{ | |
cat - | tr '[:lower:]' '[:upper:]' | |
} | |
rm -f stdout.txt | |
rm -f stderr.txt | |
{ | |
the_cmd nonswapped > >(tee stdout.txt ) 2> >(tee stderr.txt >&2 ) | |
} | to_upper | |
echo -n 'tee out: ' ; cat stdout.txt | to_upper | |
echo -n 'tee err: ' ; cat stderr.txt 1>&2 | |
echo | |
rm stdout.txt | |
rm stderr.txt | |
{ | |
{ the_cmd swapped 3>&1 1>&2 2>&3- | tee stderr.txt; } 3>&1 1>&2 2>&3- | tee stdout.txt; | |
} | to_upper | |
echo -n 'tee out: ' ; cat stdout.txt | to_upper | |
echo -n 'tee err: ' ; cat stderr.txt 1>&2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nonswapped_err | |
NONSWAPPED_OUT | |
tee out: NONSWAPPED_OUT | |
tee err: nonswapped_err | |
swapped_out | |
SWAPPED_OUT | |
tee out: SWAPPED_OUT | |
tee err: swapped_out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment