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 on…


This content originally appeared on DEV Community and was authored by AliMehraji

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 $@

Execute it like below and give the script some arguments:

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

Second One $* :

#! /bin/bash 

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

MAIN $*

The result will be :

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

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 "$@"
$ ./Diff.sh "Jhon Smith" "Marry Ann" 
$ First Parameter is Jhon Smith

The second one in double quotes "$*" :

#! /bin/bash 

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

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

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


This content originally appeared on DEV Community and was authored by AliMehraji


Print Share Comment Cite Upload Translate Updates
APA

AliMehraji | Sciencx (2025-03-08T22:48:26+00:00) Special $@ and $* Parameters in bash. Retrieved from https://www.scien.cx/2025/03/08/special-and-parameters-in-bash/

MLA
" » Special $@ and $* Parameters in bash." AliMehraji | Sciencx - Saturday March 8, 2025, https://www.scien.cx/2025/03/08/special-and-parameters-in-bash/
HARVARD
AliMehraji | Sciencx Saturday March 8, 2025 » Special $@ and $* Parameters in bash., viewed ,<https://www.scien.cx/2025/03/08/special-and-parameters-in-bash/>
VANCOUVER
AliMehraji | Sciencx - » Special $@ and $* Parameters in bash. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/08/special-and-parameters-in-bash/
CHICAGO
" » Special $@ and $* Parameters in bash." AliMehraji | Sciencx - Accessed . https://www.scien.cx/2025/03/08/special-and-parameters-in-bash/
IEEE
" » Special $@ and $* Parameters in bash." AliMehraji | Sciencx [Online]. Available: https://www.scien.cx/2025/03/08/special-and-parameters-in-bash/. [Accessed: ]
rf:citation
» Special $@ and $* Parameters in bash | AliMehraji | Sciencx | https://www.scien.cx/2025/03/08/special-and-parameters-in-bash/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.