What is $opt variable for command line parameter in bash

前端 未结 3 986
[愿得一人]
[愿得一人] 2021-01-20 10:42

I have below bash script, I am a bit confuse about what is $opt meaning. and I do not find detail information about it after search.

test.sh:

echo

3条回答
  •  清歌不尽
    2021-01-20 10:58

    From the bash(1) man page:

       for name [ [ in [ word ... ] ] ; ] do list ; done
              The list of words following in is expanded, generating a list of
              items.  The variable name is set to each element of this list in
              turn, and list is executed each time.  If the in word  is  omit‐
              ted,  the  for  command  executes  list once for each positional
              parameter that is set (see PARAMETERS below).  The return status
              is  the  exit  status of the last command that executes.  If the
              expansion of the items following in results in an empty list, no
              commands are executed, and the return status is 0.
    

    So, if the in word is missing the for loop iterates over the positional arguments to the script, i.e. $1, $2, $3, ....

    There is nothing special about the name opt, any legal variable name can be used. Consider this script and its output:

    #test.sh
    echo $*    # output all positional parameters $1, $2,...
    for x
    do
        echo $x
    done
    
    $ ./test.sh -a -b hello
    -a -b hello
    -a
    -b
    hello
    

提交回复
热议问题