简体   繁体   中英

Concatenation of * with * in bash

I am writing a method to print the name of the program in the following format:

*****************
   programName
*****************

Below is my code

#!/bin/bash

welcomeMsg(){
    simpleProgName=`echo $1 | cut -d'/' -f2 | cut -d'.' -f1`
    simpleProgNameLength=${#simpleProgName}
    asteriskCount=$((simpleProgNameLength + 3))
    simpleProgName="   $simpleProgName" 
    asteriskString="__"
    echo $asteriskCount

    for ((i=1;i<asteriskCount;i++));
    do
    asteriskString+="*"     
    echo $asteriskString
    done

    echo $asteriskString
    echo $simpleProgName
    echo $asteriskString
}
programName=$0
welcomeMsg $programName

l=$1
b=$i2
validate(){
    echo "Validation"
}

Output :

12                                                                                                                     
__*                                                                                                                    
__**                                                                                                                   
__***                                                                                                                  
__****                                                                                                                 
__*****                                                                                                                
__******                                                                                                               
__*******                                                                                                              
__********                                                                                                             
__*********                                                                                                            
__**********                                                                                                           
__***********                                                                                                          
__***********                                                                                                          
bannerSim
__***********

But when I instantiate asteriskString with "*" The output changes to

12
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim.sh
bannerSim
bannerSim.sh

ie asteriskString keeps the value of the name of the program file and concatenation of the string in the loop body does not take place, however, the loop executes. Kindly tell me why this is happening. Thanks.

A command like this one:

echo *****

prints the contents of the current directory (with some caveats).

If you don't want the contents of asteriskString to be interpreted as a potential fileglob, then you need to quote it:

echo "$asteriskString"

or better yet:

printf '%s\n' "$asteriskString"

More broadly, it is almost always a good idea to quote your variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM