bash:output:get_the_output_of_a_command
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
bash:output:get_the_output_of_a_command [2021/01/26 15:03] – peter | bash:output:get_the_output_of_a_command [2021/01/26 15:07] (current) – peter | ||
---|---|---|---|
Line 79: | Line 79: | ||
var_err=${result%this is the separator*} | var_err=${result%this is the separator*} | ||
</ | </ | ||
+ | |||
+ | ---- | ||
+ | |||
+ | ===== Only keep stderr ===== | ||
+ | |||
+ | Say you want only the stderr, but not stdout. | ||
+ | |||
+ | Then first you have to decide where you do want stdout to go: | ||
+ | |||
+ | <code bash> | ||
+ | output=$(command 2>&1 >/ | ||
+ | output=$(command 2>&1 >/ | ||
+ | output=$(command 3>&2 2>&1 1>& | ||
+ | </ | ||
+ | |||
+ | Since the last example may seem a bit confusing, here is the explanation. | ||
+ | |||
+ | * First, keep in mind that **1>& | ||
+ | * So it will be easier to analyze the following sequence: $(... 3>&2 2>&1 1>&3 3>& | ||
+ | |||
+ | ^Redirection^fd 0 (stdin)^fd 1 (stdout)^fd 2 (stderr)^fd 3^Description^ | ||
+ | |initial|/ | ||
+ | |$(...)|/ | ||
+ | |3>& | ||
+ | |2>& | ||
+ | |1>& | ||
+ | |3>& | ||
+ | |||
+ | A little note: operation **n>& | ||
+ | |||
+ | This way what the script writes to FD 2 (normally stderr) will be written to stdout because of the second redirection. | ||
+ | |||
+ | It's possible, although considerably harder, to let stdout "fall through" | ||
+ | |||
+ | <code bash> | ||
+ | exec 3>& | ||
+ | output=$(command 2>&1 1>& | ||
+ | exec 3>& | ||
+ | |||
+ | # or this alternative, | ||
+ | { output=$(command 2>&1 1>& | ||
+ | </ | ||
+ | |||
+ | In the last example above, note that **1>& | ||
+ | |||
+ | ---- | ||
+ | |||
bash/output/get_the_output_of_a_command.1611673429.txt.gz · Last modified: 2021/01/26 15:03 by peter