Home > Mobile >  what does ${@:3} means in bash?
what does ${@:3} means in bash?

Time:08-12

In the dist_train.sh from mmdetection3d, what does ${@:3} do at the last line ?

I can't understand its bash grammar.

#!/usr/bin/env bash

CONFIG=$1
GPUS=$2
NNODES=${NNODES:-1}
NODE_RANK=${NODE_RANK:-0}
PORT=${PORT:-29500}
MASTER_ADDR=${MASTER_ADDR:-"127.0.0.1"}

PYTHONPATH="$(dirname $0)/..":$PYTHONPATH \
python -m torch.distributed.launch \
    --nnodes=$NNODES \
    --node_rank=$NODE_RANK \
    --master_addr=$MASTER_ADDR \
    --nproc_per_node=$GPUS \
    --master_port=$PORT \
    $(dirname "$0")/train.py \
    $CONFIG \
    --seed 0 \
    --launcher pytorch ${@:3}

CodePudding user response:

It is standard parameter expansion:

${parameter:offset}
${parameter:offset:length}

This is referred to as Substring Expansion. It expands to up to length characters of the value of parameter starting at the character specified by offset. If parameter is @, an indexed array subscripted by @ or *, or an associative array name, the results differ as described below. If length is omitted, it expands to the substring of the value of parameter starting at the character specified by offset and extending to the end of the value. length and offset are arithmetic expressions (see Shell Arithmetic).

[...]

If parameter is @, the result is length positional parameters beginning at offset. A negative offset is taken relative to one greater than the greatest positional parameter, so an offset of -1 evaluates to the last positional parameter. It is an expansion error if length evaluates to a number less than zero.

The following examples illustrate substring expansion using positional parameters:

$ set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$ echo ${@:7}
7 8 9 0 a b c d e f g h
$ echo ${@:7:0}

$ echo ${@:7:2}
7 8
$ echo ${@:7:-2}
bash: -2: substring expression < 0
$ echo ${@: -7:2}
b c
$ echo ${@:0}
./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
$ echo ${@:0:2}
./bash 1
$ echo ${@: -7:0}

CodePudding user response:

Per the Bash Hackers wiki on the Positional Parameters syntax, the ${@:3} means any script argument starting at the third argument.

In other words, the ${@:3} syntax means "all arguments EXCEPT the first and second". A similar SO question exists from which you can infer the same conclusion.

A contrived example:

foo() {
    echo "${@:3}"
}
foo a b c d e f g h i
# prints c d e f g h i

CodePudding user response:

Great question. In bash this is one kind of something called variable expansion. In this case the variable is $@ representing all the parameters received by the program (or function), as a string.

Using the colon : means that you want to 'expand' $@ to a subset of it's original string (ie. a substring). So in this instance you're saying give me the string representing all the incoming parameters, but start from the 3rd one.

  • Related