Results 1 to 4 of 4

Thread: BASH: Calling method for each entry of string array

  1. #1
    Join Date
    Jun 2022
    Beans
    4

    Question BASH: Calling method for each entry of string array

    Hi there,

    I saw that functions have been called several times in BASH using brackets. I wanted to do the same thing, so calling my function my_func three times and passing another entry of the string array each time:

    Code:
    my_func() {
      do_something_with $1
    }
    my_func { "a" "b" "c"}
    but the method is only called once. What am I doing wrong? Did I misunderstood the bracket pattern? Don't get me wrong, I know how to create and pass an array in BASH I just wanted to use that new pattern.

    Thanks

  2. #2
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,751

    Re: BASH: Calling method for each entry of string array

    https://opensource.com/article/18/5/...ro-bash-arrays
    Code:
    my_func() {
        echo Working on $1...
    }
    a=(one two three)
    for v in ${a[@]} ; do my_func $v ; done

  3. #3
    Join Date
    Dec 2014
    Beans
    2,721

    Re: BASH: Calling method for each entry of string array

    Could you give us an example of where you've seen that ? I've written and read shell scripts for quite some time and never came across something like you describe. There's also no mention of any capability like that in the manual that I can find.

    Holger

  4. #4
    Join Date
    Jun 2022
    Beans
    4

    Lightbulb Re: BASH: Calling method for each entry of string array

    Ah, I found it. It's called the brace expression and can be used like this:

    Code:
    echo Front-{A,B,C}-Back
    which results in: Front-A-Back Front-B-Back Front-C-Back

    Sorry for my confusing question.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •