4

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.

1 Answer 1

4
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

Sign up to request clarification or add additional context in comments.

6 Comments

No Its not working Heartbeat=$(date +%Y-%m-%d$'\t'%H:%M:);grep "$Heartbeat" LOOPEUR2015-12-16.log;echo $Heartbeat;2015-12-16 08:41:;grep '2015-12-16 08:41:' LOOPEUR2015-12-16.log;2015-12-16 08:41:01,967 INFO in.FD_LOOPE_SSGA - <2220 Heartbeat No output when i use"" but we do get output when we pass as '2015-12-16 08:41:'
Check my updated answer. Also please be more specific when saying it doesn't work. Just no output, OR error messages?
thank you for specific details. Reading thru them now.
-- is only necessary if - were the first character $Heartbeat.
@user4786572 , I think chepner is right. I added -- as a last shot. I don't know why grep "$var" file doesn't return any output when grep '2015-..' file does. I'll hope that someone else can help you. (It should work). Good luck.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.