Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In a file i have a string like this which is the date of today

Sa 4. Dez

Date format is like this

stringtoday=`date +%Y-%m`

now i wanna use a if condition to check if the date of today exists in this file and if that's the case i want stop the bash script. if the date is not in this file. the script should run. How can i do this?

share|improve this question
up vote 8 down vote accepted

Something like:


DATE=$(date +%Y-%m)

if grep "$DATE" file >/dev/null; then
    echo yes
    echo no

where file is the name of your file should work. It basically uses grep to search for the value of $DATE in file, and branches depending on whether it was found or not (the exit codes of grep indicate this, see man grep "EXIT STATUS" for more information). You can use if grep …; then exit 0; fi to exit the script if the text is found to get the behaviour you want.

share|improve this answer
thanks a lot. helped me much. – NES Dec 4 '10 at 14:38
man grep. See what -q does. – geirha Feb 3 '11 at 23:11
That s cool! Thanks! – Iain Lane Feb 4 '11 at 14:14

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.