Shell shift procedure - What is this?

后端 未结 5 1788
天命终不由人
天命终不由人 2021-02-05 08:45

In shell we have the command shift, but i saw on some example its giving shift 3

Why there is a number after shift ? and what its about ? what it does ?

Example:

5条回答
  •  执笔经年
    2021-02-05 08:53

    Take a look at the man page, which says:

    shift [n]
        The  positional parameters from n+1 ... are renamed to $1 .... 
        If n is not given, it is assumed to be 1.
    

    An Example script:

    #!/bin/bash
    echo "Input: $@"
    shift 3
    echo "After shift: $@"
    

    Run it:

    $ myscript.sh one two three four five six
    
    Input: one two three four five six
    After shift: four five six
    

    This shows that after shifting by 3, $1=four, $2=five and $3=six.

提交回复
热议问题