Accessing bash command line args $@ vs $*

后端 未结 5 1938
孤城傲影
孤城傲影 2020-11-22 13:54

In many SO questions and bash tutorials I see that I can access command line args in bash scripts in two ways:

$ ~ >cat testargs.sh 
#!/bin/bash

echo \"y         


        
5条回答
  •  孤独总比滥情好
    2020-11-22 14:02

    This example let may highlight the differ between "at" and "asterix" while we using them. I declared two arrays "fruits" and "vegetables"

    fruits=(apple pear plumm peach melon)            
    vegetables=(carrot tomato cucumber potatoe onion)
    
    printf "Fruits:\t%s\n" "${fruits[*]}"            
    printf "Fruits:\t%s\n" "${fruits[@]}"            
    echo + --------------------------------------------- +      
    printf "Vegetables:\t%s\n" "${vegetables[*]}"    
    printf "Vegetables:\t%s\n" "${vegetables[@]}"    
    

    See the following result the code above:

    Fruits: apple pear plumm peach melon
    Fruits: apple
    Fruits: pear
    Fruits: plumm
    Fruits: peach
    Fruits: melon
    + --------------------------------------------- +
    Vegetables: carrot tomato cucumber potatoe onion
    Vegetables: carrot
    Vegetables: tomato
    Vegetables: cucumber
    Vegetables: potatoe
    Vegetables: onion
    

提交回复
热议问题