Printing asterisk (*) in bash shell

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

When I try to run the following code, it display 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 display as this

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

Result: 在这里输入图像描述

So I try to escape the asterisk by

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

But it still doesn't work

Any help will be greatly appreciated, thanks for all the help


You have two problems:

  • Useless Use of Echo in Backticks

  • 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".


    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.

    链接地址: http://www.djcxy.com/p/24116.html

    上一篇: 在Bash中的命令中扩展单引号内的变量

    下一篇: 在bash shell中打印星号(*)