Special $@ and $* Parameters in bash
Ali Mehraji

Ali Mehraji @cod3mason

About: DevOps/SRE Engineer. You can always access the Documents and Contribute (Help) me on github.com/cod3mas0n/Documents

Location:
Sweet Home
Joined:
Jul 4, 2022

Special $@ and $* Parameters in bash

Publish Date: Mar 8
8 2

There is a say no difference between $@ and $* in Shell! What ?!!

Have you ever thought if there is no difference, why there are two of them? it could be one $@ or $* , Do You Agree?

So Let’s see What the difference is . Create a script and try it one by one

First One $@ :

#! /bin/bash

MAIN()
{
        echo "First Parameter is $1"
}

MAIN $@
Enter fullscreen mode Exit fullscreen mode

Execute it like below and give the script some arguments:

$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon

Enter fullscreen mode Exit fullscreen mode

Second One $* :

#! /bin/bash

MAIN()
{
        echo "First Parameter is $1"
}

MAIN $*
Enter fullscreen mode Exit fullscreen mode

The result will be :

$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon
Enter fullscreen mode Exit fullscreen mode

Far Now There is no difference, maybe those people were right!

Let us go further and try them with double quotes "$@" :

#! /bin/bash

MAIN()
{
        echo "First Parameter is $1"
}

MAIN "$@"
Enter fullscreen mode Exit fullscreen mode
$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon Smith
Enter fullscreen mode Exit fullscreen mode

The second one in double quotes "$*" :

#! /bin/bash

MAIN()
{
        echo "First Parameter is $1"
}

MAIN "$*"
Enter fullscreen mode Exit fullscreen mode
$ ./Diff.sh "Jhon Smith" "Marry Ann"
First Parameter is Jhon Smith Marry Ann
Enter fullscreen mode Exit fullscreen mode

So be careful when you use double quotes and tell those people there is a difference, explain to them shell is sensitive to double quotes.

Resources

Comments 2 total

Add comment