What does $ @ mean in shell script?

0

I'm analyzing some functions in Shell Script and I came across this code:

adicionar_usuario()
{
  USUARIO=$1
  SENHA=$2

  shift; shift;

  COMENTARIO=$@
  echo useradd -c "$COMENTARIO" $USUARIO
  echo passwd $USUARIO $SENHA
  echo "$USUARIO ($COMENTARIO) com a senha $SENHA adicionado."
}

My question is what p $@ means and what shift is doing.

    
asked by anonymous 28.05.2018 / 22:42

1 answer

2

Imagine that the command to be executed will be:

$ adiciona_usuario anderson m1nh4_s3nh4 "Anderson Carlos Woss"

There will be 3 parameters. The first will be used for the user and the second for the password:

USUARIO=$1
SENHA=$2

The command $@ returns all the parameters passed to the script . In this case, it would be:

COMENTARIO="anderson m1nh4_s3nh4 \"Anderson Carlos Woss\""

Since user and password data have already been used, the command shift is used, which removes the first one from the beginning of the parameter list. Thus, running the command twice will remove both the user and the password.

The output of the program would look something like:

anderson (Anderson Carlos Woss) com a senha m1nh4_s3nh4 adicionado.
    
28.05.2018 / 22:52