Reputation: 2360
I am trying to get the output of a command set to a variable but when I run it with the Command substitution, the command returns a blank line
[user@host bin]$ ./MyAppRead 4
Segmentation fault (core dumped)
[user@host bin]$ BALLS=$(./MyAppRead 4)
[user@host bin]$ echo $BALLS
[user@host bin]$
I am expecting BALLS to be set to "Segmentation fault (core dumped)" but it is blank?
-EDIT-
Changed to reflect the advice below. But still coming back blank
[user@host bin]$ ./MyAppRead 4
Segmentation fault (core dumped)
[user@host bin]$ BALLS=$(./MyAppRead 4 2>&1)
[user@host bin]$ echo $BALLS
[user@host bin]$
Upvotes: 0
Views: 1445
Reputation: 126
Segmentation fault is a signal, and if you program gets it, it will be terminated and bash will print Segmentation fault message to shell (not program) stderr.
You can get this output by trapping segmentation fault signal with trap. Write this in file script.sh
/bin/bash
# script.sh
trap "Segmentation fault (core dumped)" 11
./MyAppRead 4
and then execute this
chmod +x script.sh
BALLS=$(./script.sh 2>&1)
echo $BALLS
Upvotes: 4
Reputation: 79546
$()
captures standard output, not standard error. A segmentation fault error will go to standard error.
If you want both, you can capture both this way:
BALLS=$(./MyAppRead 4 2>&1)
Upvotes: 6