Hi I need help to know how can we pass the vairble 2015-12-16 08:16: to grep command
bash-3.2$ Heartbeat=$(date +%Y-%m-%d$'\t'%H:%M:)
bash-3.2$ echo $Heartbeat
2015-12-16 08:16:
bash-3.2$ grep $Heartbeat file.txt is not working correct, but
grep '2015-12-16 08:16:' file.txt works correctly.
Please help me how can we do this for variable.
grep -- "$Heartbeat" file
single quotes prevent variable expansion, double quotes allow variable expansion and keep space delimited value together as one argument.
In your case, we also need to add -- (end of arguments) as your $Heartbeat value contains - chars, which maybe being interpreted as grep options.
IHTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With