Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass pattern from variable to grep command

Tags:

grep

bash

unix

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.

like image 596
user4786572 Avatar asked Oct 21 '25 05:10

user4786572


1 Answers

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

like image 109
shellter Avatar answered Oct 23 '25 22:10

shellter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!