6
a=5
echo "*/$aMin * * * * bash /etc/init.d/ckDskCheck.sh"

When I try to run the following code, it displays properly

*/5 * * * * bash /etc/init.d/ckDskCheck.sh

But when I try to assign the result using the following code to the variable and print it out, it displays as this:

a=5
cronSen=`echo "*/$a * * * * bash /etc/init.d/ckDskCheck.sh"`
echo $cronSen

Result:

enter image description here

So I try to escape the asterisk by

cronSen=`echo "\*/$a \* \* \* \* bash /etc/init.d/ckDskCheck.sh"`

But it still doesn't work. Why? How can I fix this?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
user2499325
  • 419
  • 1
  • 5
  • 15
  • 1
    Quote `$cronSen` when you echo it. – Etan Reisner Aug 13 '14 at 02:19
  • 2
    Quote all variables that don't have a specific reason to be unquoted. – n. m. could be an AI Aug 13 '14 at 02:29
  • @EtanReisner,@n.m Thanks for the reply, it works now! But just curious to know when should I put/not put the quotes? Is there any norms to follow? – user2499325 Aug 13 '14 at 02:59
  • 1
    Are you sure you use bash? My bash not show like you desribed. Anyway you need do echo "$varname" if you want it be not substituted *. – arheops Aug 13 '14 at 03:03
  • @user2499325 As tripleee indicates in his answer. Quote everything you use as part of a command that you run unless you know you want the shell to perform word splitting and other expansions on it. – Etan Reisner Aug 13 '14 at 12:53
  • Why would you run an `init.d` script from Cron, though? Either it's in the wrong location or else you are doing something moderately funky. – tripleee Aug 13 '14 at 14:21

2 Answers2

8

You have two problems:

  1. Useless Use of Echo in Backticks

  2. Always quote what you echo

So the fixed code is

a=5
cronSen="*/$a * * * * bash /etc/init.d/ckDskCheck.sh"
echo "$cronSen"

It appears you may also have a Useless Use of Variable, but perhaps cronSen is useful in a larger context.

In short, quote everything where you do not require the shell to perform token splitting and wildcard expansion.

Token splitting;

 words="foo bar baz"
 for word in $words; do
      :

(This loops three times. Quoting $words would only loop once over the literal token foo bar baz.)

Wildcard expansion:

pattern='file*.txt'
ls $pattern

(Quoting $pattern would attempt to list a single file whose name is literally file*.txt.)

In more concrete terms, anything containing a filename should usually be quoted.

A variable containing a list of tokens to loop over or a wildcard to expand is less frequently seen, so we sometimes abbreviate to "quote everything unless you know precisely what you are doing".

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Copied choice parts of this answer to http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable/27701642#27701642 – tripleee Dec 30 '14 at 07:58
5

You must use double quote when echo your variable:

echo "$cronSen"

If you don't use double quote, bash will see * and perform filename expansion. * expands to all files in your current directory.

cuonglm
  • 2,766
  • 1
  • 22
  • 33