Save output of a Mongo Command to a Variable bash script I have been writing a script to find and kill slow mongodb queries, how can I store output of db.currentOp() in a variable?
current I am using this but failing
#! /bin/bash

out=&quot;&quot;
mongo &lt;&lt; EOF

out=db.currentOp();
EOF
echo&quot;----------------------------&quot;
echo&quot;$out&quot;

Updated Script
#! /bin/bash

out=$(mongo --eval 'db.currentOp({op:&quot;query&quot;})')
echo&quot;-----------------------&quot;
echo&quot;$out&quot;

Output
./kill_slow_mongo_queries.sh: line 4: echo-----------------------: command not found
./kill_slow_mongo_queries.sh: line 5: echoMongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { &quot;id&quot; : UUID(&quot;ce96f3ee-f5e1-49b5-aeef-b5766328dda3&quot;) }
MongoDB server version: 3.6.8
{ &quot;inprog&quot; : [ ], &quot;ok&quot; : 1 }: No such file or directory
Mongo -command have multiple parameters that are handy in this case.
'--eval' let you run commands and '--quiet' keep all clutter away
out=$(mongo --quiet --eval=&quot;db.currentOp()&quot;) ; echo $out