Directing cat or grep command in variable

Hi,

I still have the problem with directing information from cat or grep to a variable.

For instance:

XMSG "$(date +%Y_%m_%d)_error_report.txt" "$(cat "$(date +%Y_%m_%d)_error_report.txt")" &

Works! The text received by cat is directed to my function.

If it is written like this, my variable var is empty

var=$(cat "$(date +%Y_%m_%d)_error_report.txt")

Same effect here:

var=$(grep "sdhdjhf" text.txt)

Can anybody please explain why the redirection does not work?

I would doubt on the multiple lines that cat file and grep something file would return :slight_smile:

Or non-existence of the file in case of cat and nothing found in case of grep

It oughta work. Seems to work here with this test scenario. Similar to previous response, two likely causes would be "file is empty" or "grep didn't find anything".

$cat temp.sh
echo "Line 1"  > lines.x
echo "Line 2" >> lines.x
x=`cat lines.x`
echo x = $x
y=$(cat lines.x)
echo y = $y
rm lines.x

$./temp.sh
x = Line 1 Line 2
y = Line 1 Line 2