'shell's command substitution not work : isSuc=$(db2 "update table_x set x=123"|grep successfully)

This command can be executed successfully in my db2 server:

~]# db2 "update table_x set x=xxx"|grep successfully

With output:

~]# DB20000I  The SQL command completed successfully

but if I execute this command:

~]# isSuc=$(db2 "update table_x set x=123"|grep successfully)

or:

~]# echo $(db2 "update table_x set x=123"|grep successfully)

isSuc does not get a value, and the echo outputs nothing. Why is this?



Solution 1:[1]

Suggesting to try this:

 echo $( { db2 "update table_x set x=123" |grep successfully; } 2>&1 )

Solution 2:[2]

.../> echo $(db2 "update table_x set x=123")
DB20000I The SQL command completed successfully.

.../> echo $(db2 "update table_x set x=123"|grep DB)
DB21034E The command was processed as an SQL statement because it was not a

.../> echo $(db2 "update table_x set x=123"|cat)
DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned: SQL1024N A database connection does not exist. SQLSTATE=08003

.../> echo $(db2 connect to db_name &>/dev/null;db2 "update table_x set x=123"|grep successfully;)
DB20000I The SQL command completed successfully.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chepner
Solution 2