Loop through variables in bash script

Is there a way to loop through all the in scope variables in bash?

I saw the below example for powershell:

foreach($variable in $OctopusParameters)
{
    # Perform the desired action here e.g.
    Write-Output $variable
}

Thanks, Simon

Hi Simon,
Unfortunately I have some bad news for you on this question. For PowerShell and C# we generate a bootstrap script which contains helper methods (like Set-OctopusVariable) and adds the variables into local dictionary objects so that they can be accessed by your code.
User provided script can then easily access variables in these environments using the familiar syntax of

$OctopusParameters[“MyApp.ConnectionString”]

(for PowerShell) or

Octopus.Parameters[“MyApp.ConnectionString”];

(for C#).

As you can see in our documentation on writing custom scripts, the bash format is slightly different. The bash bootstrap file actually provides access to the variables by calling

$(get_octopusvariable “MyApp.ConnectionString”)

Although Bash 4+ supports a dictionary-like object this is not available in Bash 3 which is our lowest supported version. Internally the variable access is wrapped in the aforementioned get_octopusvariable function which contains a large case statement that returns the value based on the variable name.

What this means, is that the variables are unfortunately currently unable to be iterated over from within a bash script. Providing this functionality would require some re-thinking about how we could “fake” a dictionary. If this is a feature that you feel strongly about, I might suggest that you could add a UserVoice ticket outlining its requirements and your thoughts. In this way we can see if there is a strong demand from the community, as well as get suggestions and tips on how it should work.

Thanks for dropping us a line, but I’m sorry that this is probably not the response you were hoping to hear.
What in particular were you hoping to achieve by looping over the parameters? Perhaps we can think of another way that might still solve your problem
Cheers,
Robert

How about a slightly different tact on this then, since mono is running on the Linux server for Calamari, can I access with C#?

Thanks, Simon

I was able to do what I need in C#:

foreach(var p in Octopus.Parameters)
{
    Console.WriteLine("{0} = {1}", p.Key, p.Value);
}

So I am good on this.

Thanks.

Hi Simon,
Great to hear you found a solution! Thanks for letting us know that all is well again.
Cheers,
Robert