Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6009171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:55:57+00:00 2026-05-23T01:55:57+00:00

The $@ variable seems to maintain quoting around its arguments so that, for example:

  • 0

The $@ variable seems to maintain quoting around its arguments so that, for example:

$ function foo { for i in "$@"; do echo $i; done }
$ foo herp "hello world" derp
herp
hello world
derp

I am also aware that bash arrays, work the same way:

$ a=(herp "hello world" derp)
$ for i in "${a[@]}"; do echo $i; done
herp
hello world
derp

What is actually going on with variables like this? Particularly when I add something to the quote like “duck ${a[@]} goose”. If its not space separated what is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:55:58+00:00Added an answer on May 23, 2026 at 1:55 am

    Usually, double quotation marks in Bash mean “make everything between the quotation marks one word, even if it has separators in it.” But as you’ve noticed, $@ behaves differently when it’s within double quotes. This is actually a parsing hack that dates back to Bash’s predecessor, the Bourne shell, and this special behavior applies only to this particular variable.

    Without this hack (I use the term because it seems inconsistent from a language perspective, although it’s very useful), it would be difficult for a shell script to pass along its array of arguments to some other command that wants the same arguments. Some of those arguments might have spaces in them, but how would it pass them to another command without the shell either lumping them together as one big word or reparsing the list and splitting the arguments that have whitespace?

    Well, you could pass an array of arguments, and the Bourne shell really only has one array, represented by $* or $@, whose number of elements is $# and whose elements are $1, $2, etc, the so-called positional parameters.

    An example. Suppose you have three files in the current directory, named aaa, bbb, and cc c (the third file has a space in the name). You can initialize the array (that is, you can set the positional parameters) to be the names of the files in the current directory like this:

    set -- *
    

    Now the array of positional parameters holds the names of the files. $#, the number of elements, is three:

    $ echo $#
    3
    

    And we can iterate over the position parameters in a few different ways.


    1) We can use $*:

    $ for file in $*; do
    >    echo "$file"
    > done
    

    but that re-separates the arguments on whitespace and calls echo four times:

    aaa
    bbb
    cc
    c
    

    2) Or we could put quotation marks around $*:

    $ for file in "$*"; do
    >    echo "$file"
    > done
    

    but that groups the whole array into one argument and calls echo just once:

    aaa bbb cc c
    

    3) Or we could use $@ which represents the same array but behaves differently in double quotes:

    $ for file in "$@"; do
    >     echo "$file"
    > done
    

    will produce

    aaa
    bbb
    cc c
    

    because $1 = “aaa”, $2 = “bbb”, and $3 = “cc c” and "$@" leaves the elements intact. If you leave off the quotation marks around $@, the shell will flatten and re-parse the array, echo will be called four times, and you’ll get the same thing you got with a bare $*.


    This is especially useful in a shell script, where the positional parameters are the arguments that were passed to your script. To pass those same arguments to some other command — without the shell resplitting them on whitespace — use "$@".

    # Truncate the files specified by the args
    rm "$@"
    touch "$@"
    

    In Bourne, this behavior only applies to the positional parameters because it’s really the only array supported by the language. But you can create other arrays in Bash, and you can even apply the old parsing hack to those arrays using the special "${ARRAYNAME[@]}" syntax, whose at-sign feels almost like a wink to Mr. Bourne:

    $ declare -a myarray
    $ myarray[0]=alpha
    $ myarray[1]=bravo
    $ myarray[2]="char lie"
    $ for file in "${myarray[@]}"; do echo "$file"; done
    alpha
    bravo
    char lie
    

    Oh, and about your last example, what should the shell do with "pre $@ post" where you have $@ within double quotes but you have other stuff in there, too? Recent versions of Bash preserve the array, prepend the text before the $@ to the first array element, and append the text after the $@ to the last element:

    pre aaa
    bb
    cc c post
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems to be generally considered poor programming practise to use variable names that
It seems uninitialized global variable is treated as weak symbol in Gcc. What is
It seems that the server is limited at ~32720 sockets... I have tried every
It seems logical to do some optimization around static final constants ( e.g. replace
Seems like a simple problem, but can't get this to work. In the example
It seems that these code snippets ought to behave identically: 1: Monitor.TryEnter(object) if (Monitor.TryEnter(lockObject))
I maintain an application that sends me an email when an error occurs in
I know that this question seems illogical, but I have to try, and I
If we know the type of variable or parameter very well, why not to
the index variable below is incorrectly initialized because f() will be returning stuff other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.