Sample Header Ad - 728x90

How to redirect stderr in a variable but keep stdout in the console

12 votes
4 answers
20271 views
My goal is to call a command, get stderr in a variable, but keep stdout (and only stdout) on the screen. Yep, that's the opposite of what most people do :) For the moment, the best I have is : #!/bin/bash pull=$(sudo ./pull "${TAG}" 2>&1) pull_code=$? if [[ ! "${pull_code}" -eq 0 ]]; then error "[${pull_code}] ${pull}" exit "${E_PULL_FAILED}" fi echo "${pull}" But this can only show the stdout in case of success, and after the command finish. I want to have stdout on live, is this possible ? **EDIT** Thanks to @sebasth, and with the help of https://unix.stackexchange.com/questions/430161/redirect-stderr-and-stdout-to-different-variables-without-temporary-files , I write this : #!/bin/bash { sudo ./pull "${TAG}" 2> /dev/fd/3 pull_code=$? if [[ ! "${pull_code}" -eq 0 ]]; then echo "[${pull_code}] $(cat<&3)" exit "${E_PULL_FAILED}" fi } 3<
Asked by Doubidou (223 rep)
Oct 9, 2018, 08:46 AM
Last activity: Jun 10, 2025, 06:30 PM