aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZhiming Wang <zmwangx@gmail.com>2015-07-15 09:52:59 -0700
committerZhiming Wang <zmwangx@gmail.com>2015-07-15 09:52:59 -0700
commit2adbde845c778e34b140f514bc2dac09688b97e8 (patch)
tree3f4b713d3215492652dc9618630acac3a735def0
parentc5bbf8bf924e79e52a852b0e5bfb433f73ef3d3f (diff)
downloadmy_new_personal_website-2adbde845c778e34b140f514bc2dac09688b97e8.tar.xz
my_new_personal_website-2adbde845c778e34b140f514bc2dac09688b97e8.zip
20150715 Zsh: save stdout, stderr, and return value of command to different variables (without temp file)
Diffstat (limited to '')
-rw-r--r--source/blog/2015-07-15-zsh-save-stdout-stderr-and-return-value-of-command-to-different-variables-without-temp-file.md33
1 files changed, 33 insertions, 0 deletions
diff --git a/source/blog/2015-07-15-zsh-save-stdout-stderr-and-return-value-of-command-to-different-variables-without-temp-file.md b/source/blog/2015-07-15-zsh-save-stdout-stderr-and-return-value-of-command-to-different-variables-without-temp-file.md
new file mode 100644
index 00000000..d27a3002
--- /dev/null
+++ b/source/blog/2015-07-15-zsh-save-stdout-stderr-and-return-value-of-command-to-different-variables-without-temp-file.md
@@ -0,0 +1,33 @@
+---
+title: "Zsh: save stdout, stderr, and return value of command to different variables (without temp file)"
+date: 2015-07-15T09:21:47-07:00
+date-display: July 15, 2015
+---
+This is something worth sharing. The idea was based on [this SO answer](http://stackoverflow.com/a/18086548/1944784), and I cooked up this particular implementation to remove potential race conditions, with input from Mathias Fredriksson[^1]. See [mafredri/zsh-async#1](https://github.com/mafredri/zsh-async/issues/1), and in particular [this comment](https://github.com/mafredri/zsh-async/issues/1#issuecomment-121468958) for explanation.
+
+```zsh
+# The following construct evaluates "$@" and saves output on stdout in the
+# parameter stdout, output on stderr in the parameter stderr, and return value
+# in the parameter return.
+#
+# The idea was based on http://stackoverflow.com/a/18086548/1944784, but this
+# implementation is completely race-condition-free. The implementation was
+# refined during my exchange with Mathias Fredriksson @mafredri, in
+# https://github.com/mafredri/zsh-async/issues/1. See mainly
+# https://github.com/mafredri/zsh-async/issues/1#issuecomment-121468958, where
+# the advantage of this implementation is explained.
+
+unset stdout stderr ret
+eval "
+$(
+ {
+ stdout=$(eval "$@")
+ ret=$?
+ typeset -p stdout ret
+ } 2> >(stderr=$(cat); typeset -p stderr)
+)"
+```
+
+Also available as [a gist](https://gist.github.com/zmwangx/efababea6258cedea07a).
+
+[^1]: Mathias ([\@mafredri](https://github.com/mafredri)) is the author of the lovely [zsh-async](https://github.com/mafredri/zsh-async) library, and a maintainer of [sindresorhus/pure](https://github.com/sindresorhus/pure). He forever revolutionalized [my prompt](https://github.com/zmwangx/prezto/blob/master/modules/prompt/functions/prompt_zmwangx_setup).