What does $@ mean in shell script?

Asked

Viewed 212 times

0

I’m analyzing some functions in Shell Script and 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 doubt is what $@ means and what shift is making.

1 answer

1


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

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

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

As user and password data have already been used, the command is used shift, which removes the first from the beginning of the parameter list. Thus, executing the command twice will be removed both the user and the password.

The output of the program would be something like:

anderson (Anderson Carlos Woss) com a senha m1nh4_s3nh4 adicionado.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.