Search code examples
macoscronoperating-systemenvironment-variablessystem

How to export variable which includes * (asterisk) sign in it?


I have a cron job environment variable which I wanna export.

    export CRON="30 * * * * *"

The problem is * (asterisk) brings all file name in that folder. Which end up with an error. For a quick try and understand the issue do followings.

  1. Open terminal (I am using Mac)
  2. export TEST="30 *"
  3. echo $TEST
  4. Check the output

Here Output will not

30 *

It's

30 and all your files in that folder.

PS:- This environment variable is getting read by one application and needs to use this in some function. ex :-

cron.schedule(CRON, function(){// some code handling})

Solution

  • Your export command is okay and does not cause the asterisks to be expanded. The problem is your echo $TEST. The rules for POSIX shells cause the expansion of $TEST to occur before glob expansion. So what you are doing is actually this:

    echo 30 *
    

    If you quote the expansion to inhibit the glob expansion you will see what you expect:

    echo "$TEST"
    

    Your question is a good example why the POSIX 1003 shell behavior is awful. And so many new, innovative, shells like fish and elvish don't implement that behavior.