Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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})
like image 924
Ravi Prakash Avatar asked Nov 01 '25 04:11

Ravi Prakash


1 Answers

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.

like image 114
Kurtis Rader Avatar answered Nov 04 '25 07:11

Kurtis Rader



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!