First thing that we need to keep in mind is that in Linux everything is treated as a file. Also each open file is assigned a unique number called file descriptor.
Also there are 3 standard streams. Each one of them as a unique file descriptor
- The file descriptor for standard input is 0 (zero)
- The file descriptor for standard output is 1 (one)
- The file descriptor for standard error is 2 (two)
Let’s take one example, below we are trying to store error from our command to result variable.

As you can see that error message is printed in terminal rather than in the variable, this is because command substitution works only for stdout.
Let’s now redirect stderr to the stdout. This will print both stdout and stderr to variable.

Finally let’s just store stderr in a variable.

This way we can store only stderr in a variable.
Hope you will find it useful.
Thank you.