DEV Community

Cover image for Special $@ and $* Parameters in bash
AliMehraji
AliMehraji

Posted on • Edited on

6 1 1

Special $@ and $* Parameters in bash

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

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (2)

Collapse
 
dionisiodev profile image
Dionisio

I learned something here.

Collapse
 
vlasales profile image
Vlastimil Pospichal

Double Quotes are necessary.