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

  • SEARCH
  • Home
  • 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 8620131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:30:39+00:00 2026-06-12T06:30:39+00:00

Before explaining my bash problem let me give you some context: I am writing

  • 0

Before explaining my bash problem let me give you some context:

I am writing some scripts using a bash “framework” we use at my current job. One of the feature of the framework is to init a sets of environments variables useful to run jobs on our cluster infrastructure.

These variables depend on a date specified by $YY, $mm and $dd which are also environment variables (yes, this is wired). To use the framework you start by defining the date and then you call a function to init other vars. This works fine when you write scripts that need variables for a specific day only. Today I am writing something which needs variables for 2 different days. Writing this I face a strange issue. For you to better understand the problem I wrote this code that simulate it:

#!/bin/bash

function assign(){
    date=$1
    date[1]=$YY
    date[2]=$mm
    date[3]=$dd
}

function display() {
    date=$1
    echo "${date[1]}/${date[2]}/${date[3]}"
}

export YY=2012
export mm=09
export dd=20
declare -a my_date1=()
assign $my_date1

export YY=2012
export mm=08
export dd=20
declare -a my_date2=()
assign $my_date2

display $my_date1
display $my_date2

The expected output is:

2012/09/20
2012/08/20

But the output is:

2012/08/20
2012/08/20

At first I thought that the assign function filled the array with reference to $YY, $mm and $dd instead of their values. But then I try with the following code and it doesn’t change the result.

date[1]=$(echo $YY)
date[2]=$(echo $mm)
date[3]=$(echo $dd)

Can somebody explain me what append?
Maybe something wired with date=$1…

  • 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-06-12T06:30:40+00:00Added an answer on June 12, 2026 at 6:30 am

    Arrays are not passed by either value or reference in bash. Rather, the value of the expansion of the array is passed by value. When you write

    assign $my_date1
    

    the date variable inside assign is null, since $my_date1 expands to an empty string and disappears after word-splitting before the function is called. As a result, $1 is unset.

    But date, being a global variable because it was not declared as local, is set correctly using YY et al, then reset on the second call to assign.

    Also, note that the first line of your functions does not make date a reference to the argument; it’s really just setting the 0th element of what becomes the global date array to the expansion of $1.


    Having said I’ll that, I’ll show you a way to fake it using the declare built-in and indirect parameter expansion.

    function assign () {
        ref=$1
        # Without the -g, we'd declare function-local parameters. The argument is a
        # string to evaluate as a variable assignment. If $ref=my_date1, then we do
        # 'my_date1[1]=$YY', 'my_date1[2]=$mm', etc.
        declare -g "$ref[1]=$YY"
        declare -g "$ref[2]=$mm"
        declare -g "$ref[3]=$dd"
    }
    
    function display () {
        ref=$1
        # If $ref=my_date1, then idx
        # iterates over my_date[1], my_date[2], my_date[3].
        # E.g. ${!idx} in the first iteration is ${my_date[1]}.
        arr=()
        for idx in $ref[{1,2,3}]; do
            arr+=( ${!idx} )
        done
        local IFS="/"
        echo "${arr[*]}"
    }
    
    export YY=2012 mm=09 dd=20
    assign my_date1    # The *name* of the array; no need to predeclare
    
    export YY=2012 mm=08 dd=20
    assign my_date2    # The *name* of the array; no need to predeclare
    
    # Again, just the *names* of the arrays
    display my_date1
    display my_date2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening everyone, before explaining my problem, I should give you some explanation on
Before explaining the problem I am facing,this is the code I am using: <!DOCTYPE
Before I start explaining the code I will first give my use case so
Before asking my question, let me explain the context. CONTEXT: I have a web
This has some lengthy background before the actual question, however, it bears some explaining
I'll start with the background story before explaining the problem with my code. I'm
First let me start by explaining my use case: Say there is a database
Before asking the question let me preface with the fact that I am new
Before, I was using VS 2008 command prompt and when I do clrver I
Before I ask the question let me state that I have attempted to google

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.