I am working on a BASH script which creates automated backups via the crontab. I am trying to use

myVARIABLE=`grep backup /etc/crontab`

to return lines containing "backup" if an entry has already been made. Instead of returning lines from /etc/crontab it seems to be returning the output of multiple ls commands. I have run the command

grep backup /etc/crontab

from the command line and it works as expected. I can also grep other files via my BASH script and it returns expected results as well. Any thoughts as to why /etc/crontab causes grep problems within a script? Thanks!

  • 5
    Chances are you are using myVARIABLE or quoting something incorrectly but without more information it's impossible to tell. Try adding snippets about what your script does after that line. Also make sure to describe the high-level goal you are trying to accomplish so people can suggest good ways to go about it. Putting multiple lines in a shell variable is clunky at best.
    – jw013
    Dec 15, 2011 at 2:18

1 Answer 1


That is because /etc/crontab contains asterisk characters (*), which, when not escaped, will be expanded by the shell.

Compare the output of these two commands:

echo $myVariable
echo "$myVariable"

The first command uses the variable unescaped, so the asterisk characters will be expanded to all files in the current directory. The second command is what you want, the real contents of the crontab file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .