For example, to store the standard error output of the ls command to a variable named errors, you can use the following command:errors=$(ls non-existent-file 2>&1) Store Standard Error in a Bash VariableAlternatively, you can use the $? special parameter to store the exit status of a command to a variable. The exit status is a numeric value that indicates whether the command was successful or not. A value of 0 indicates success, while a non-zero value indicates an error. For example, to store the exit status of the ls command to a variable named status, you can use the following command:ls non-existent-file status=$? You can then use the $status variable to check the exit status of the ls command and take appropriate action based on the result. For example: #!/usr/bin/env bash # Pur your commands here if [ $status -ne 0 ]; then echo “Last command failed with an error.” fi123456#!/usr/bin/env bash # Pur your commands hereif [ $status -ne 0 ]; then  echo “Last command failed with an error.“fi

Keep in mind that the $() command substitution syntax allows you to execute a command and substitute its output in place. The 2> operator redirects the standard error output of the command to the &1 stream, which is the standard output stream. This allows you to capture both the standard output and standard error output of the command in a single variable.

How to Store Standard Error to a Variable in Bash   TecAdmin - 53