Redirect of stdout and stderr work on linux, osx and unix. Because of this is not under `Linux things...` section! :)
# To suppress error:
./somescript.sh 2> /dev/null# To suppress out:
./somescript.sh 1> /dev/null# To suppress both:
./somescript.sh 2>&1 > /dev/nullIn Bash, you can redirect error output (stderr) into a variable using a process substitution or a temporary
file. Here are a few methods to achieve this:
### Method 1: Using Process Substitution
You can use process substitution to capture the error output into a variable:
```bash
ERROUT=$(ls -l somefile.txt 2>&1 >/dev/null)
```
However, this captures both stdout and stderr. If you want to capture only stderr, you can use:
```bash
ERROUT=$(ls -l somefile.txt 2>&1 >/dev/null)
```
But this is not very clean. A better approach is to use a temporary file or a named pipe.
### Method 2: Using a Temporary File
Create a temporary file to capture the error output, then read it into a variable:
```bash
# Create a temporary file
TEMP_FILE=$(mktemp)
# Redirect stderr to the temporary file
ls -l somefile.txt 2> "$TEMP_FILE"
# Read the error output into the variable
ERROUT=$(cat "$TEMP_FILE")
# Remove the temporary file
rm "$TEMP_FILE"
# Print the error output
echo "Error output: $ERROUT"
```
### Method 3: Using a Named Pipe (FIFO)
You can use a named pipe to capture the error output:
```bash
# Create a named pipe
PIPE=$(mktemp -u)
mkfifo "$PIPE"
# Redirect stderr to the named pipe in the background
ls -l somefile.txt 2> "$PIPE" &
# Read the error output from the named pipe into the variable
ERROUT=$(cat "$PIPE")
# Remove the named pipe
rm "$PIPE"
# Print the error output
echo "Error output: $ERROUT"
```
### Method 4: Using a Function
You can create a function to capture the error output:
```bash
capture_error() {
local cmd="$1"
local output
output=$(eval "$cmd" 2>&1 >/dev/null)
echo "$output"
}
ERROUT=$(capture_error "ls -l somefile.txt")
echo "Error output: $ERROUT"
```
### Method 5: Using `exec` to Redirect stderr
You can use `exec` to redirect stderr to a file descriptor, then read it into a variable:
```bash
# Redirect stderr to file descriptor 3
exec 3>&2
ls -l somefile.txt 2>&3 | while read -r line; do
ERROUT+="$line"
done
# Reset stderr
exec 3>&-
echo "Error output: $ERROUT"
```
### Best Approach
The most straightforward and commonly used method is to use a temporary file or a named pipe. Here's a clean
example using a temporary file:
```bash
# Create a temporary file
TEMP_FILE=$(mktemp)
# Redirect stderr to the temporary file
ls -l somefile.txt 2> "$TEMP_FILE"
# Read the error output into the variable
ERROUT=$(cat "$TEMP_FILE")
# Remove the temporary file
rm "$TEMP_FILE"
# Print the error output
echo "Error output: $ERROUT"
```
### Example with a Script
If you want to capture the error output of a script, you can do:
```bash
# Create a temporary file
TEMP_FILE=$(mktemp)
# Run the script and redirect stderr to the temporary file
./yourscript.sh 2> "$TEMP_FILE"
# Read the error output into the variable
ERROUT=$(cat "$TEMP_FILE")
# Remove the temporary file
rm "$TEMP_FILE"
# Print the error output
echo "Error output: $ERROUT"
```
### Summary
- **Process Substitution**: Simple but captures both stdout and stderr.
- **Temporary File**: Clean and straightforward.
- **Named Pipe**: Useful for more complex scenarios.
- **Function**: Reusable and flexible.
- **exec**: Advanced but powerful.
Choose the method that best fits your needs. The temporary file method is often the most straightforward and
reliable for capturing error output into a variable.
file. Here are a few methods to achieve this:
### Method 1: Using Process Substitution
You can use process substitution to capture the error output into a variable:
```bash
ERROUT=$(ls -l somefile.txt 2>&1 >/dev/null)
```
However, this captures both stdout and stderr. If you want to capture only stderr, you can use:
```bash
ERROUT=$(ls -l somefile.txt 2>&1 >/dev/null)
```
But this is not very clean. A better approach is to use a temporary file or a named pipe.
### Method 2: Using a Temporary File
Create a temporary file to capture the error output, then read it into a variable:
```bash
# Create a temporary file
TEMP_FILE=$(mktemp)
# Redirect stderr to the temporary file
ls -l somefile.txt 2> "$TEMP_FILE"
# Read the error output into the variable
ERROUT=$(cat "$TEMP_FILE")
# Remove the temporary file
rm "$TEMP_FILE"
# Print the error output
echo "Error output: $ERROUT"
```
### Method 3: Using a Named Pipe (FIFO)
You can use a named pipe to capture the error output:
```bash
# Create a named pipe
PIPE=$(mktemp -u)
mkfifo "$PIPE"
# Redirect stderr to the named pipe in the background
ls -l somefile.txt 2> "$PIPE" &
# Read the error output from the named pipe into the variable
ERROUT=$(cat "$PIPE")
# Remove the named pipe
rm "$PIPE"
# Print the error output
echo "Error output: $ERROUT"
```
### Method 4: Using a Function
You can create a function to capture the error output:
```bash
capture_error() {
local cmd="$1"
local output
output=$(eval "$cmd" 2>&1 >/dev/null)
echo "$output"
}
ERROUT=$(capture_error "ls -l somefile.txt")
echo "Error output: $ERROUT"
```
### Method 5: Using `exec` to Redirect stderr
You can use `exec` to redirect stderr to a file descriptor, then read it into a variable:
```bash
# Redirect stderr to file descriptor 3
exec 3>&2
ls -l somefile.txt 2>&3 | while read -r line; do
ERROUT+="$line"
done
# Reset stderr
exec 3>&-
echo "Error output: $ERROUT"
```
### Best Approach
The most straightforward and commonly used method is to use a temporary file or a named pipe. Here's a clean
example using a temporary file:
```bash
# Create a temporary file
TEMP_FILE=$(mktemp)
# Redirect stderr to the temporary file
ls -l somefile.txt 2> "$TEMP_FILE"
# Read the error output into the variable
ERROUT=$(cat "$TEMP_FILE")
# Remove the temporary file
rm "$TEMP_FILE"
# Print the error output
echo "Error output: $ERROUT"
```
### Example with a Script
If you want to capture the error output of a script, you can do:
```bash
# Create a temporary file
TEMP_FILE=$(mktemp)
# Run the script and redirect stderr to the temporary file
./yourscript.sh 2> "$TEMP_FILE"
# Read the error output into the variable
ERROUT=$(cat "$TEMP_FILE")
# Remove the temporary file
rm "$TEMP_FILE"
# Print the error output
echo "Error output: $ERROUT"
```
### Summary
- **Process Substitution**: Simple but captures both stdout and stderr.
- **Temporary File**: Clean and straightforward.
- **Named Pipe**: Useful for more complex scenarios.
- **Function**: Reusable and flexible.
- **exec**: Advanced but powerful.
Choose the method that best fits your needs. The temporary file method is often the most straightforward and
reliable for capturing error output into a variable.