help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is there a way to save stdout and stderr to two bash variables?


From: Koichi Murase
Subject: Re: is there a way to save stdout and stderr to two bash variables?
Date: Tue, 12 May 2020 09:45:06 +0900

2020-05-12 6:13 Peng Yu <address@hidden>:
>
> But this solution still involves using external files if I read the
> code correctly? Thanks.

Yes, as already explained in the original post of `ble/util/assign'.
If you don't want to use external files, you can instead use nested
command substitutions and variable exports by `declare -p':

  function cmd {
    echo this is stderr >&2
    echo this is stdout
  }

  eval -- "$(
    { stderr=$(
        { stdout=$(cmd); } 2>&1
        declare -p stdout >&3); } 3>&1
    declare -p stderr )"

  echo "($stdout)($stderr)"

If you feel it is cumbersome to write it every time, again you can
wrap it in a function:

  function upvars {
    while (($#)); do
      unset "$1"
      printf -v "$1" %s "$2"
      shift 2
    done
  }
  function save-stdout-stdin {
    eval -- "$(
      { printf -v "$2" %s "$(
        { printf -v "$1" %s "$(eval -- "$3")"; } 2>&1
        declare -p "$1" >&3)"; } 3>&1
      declare -p "$2" )"
    upvars "$1" "${!1}" "$2" "${!2}"
  }

  save-stdout-stdin a b cmd
  echo "($a)($b)"

However, these solutions require at least three forks which are slower
than the external file accesses.  There is no solution with neither
forks nor external file accesses.  In my opinion, there is no reason
to refrain from external files as far as the files are created in
memory (tmpfs such as /dev/shm or /tmp) and the permissions are
properly mainined.

Also, if you do not allow even internal usages of external files at
all, you cannot use here documents and here strings as they also use
temporary files internally.  For example, you can confirm this by the
following command.

$ ls -la /dev/fd/0 <<< Here
lr-x------. 1 murase murase 64 2020-05-12 09:42:22 /dev/fd/0 ->
/tmp/sh-thd.ZLmXgN (deleted)

--
Koichi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]