Home > OS >  How to have a list of strings that are tied to another variable in bash?
How to have a list of strings that are tied to another variable in bash?

Time:11-24

Forgive me, I am very new to programming in general, and I'm not sure if the title even makes sense. I'm just wondering if anyone can point me in the right direction on what I'm trying to do:

What I want to do is have a set of $variables, for instance $one, $two, $three, etc.

And $one[1] would return "a", $one[2] would return "b", etc.

Is there any easy way to do this in bash?

I thought 2d array, but bash doesn't really have that, so I'm wondering if anyone has suggestions.

Right now the only thing I can think of is using a lot of case statements, but I would really prefer the code to be as clean as I can get it.

Thank you all for the help! Implementation that worked for me in case anyone else ever needs help with this:

    example=(a b c d e)
    read selection
    #writing to file:
    printf ${example[$selection]} > file;; 

CodePudding user response:

The solution is:

S{one:<INDEX_0_BASED>:1}

So, ${one:0:1} will return a and ${one:1:1} will return b.

Here https://github.com/stevemac321/pdfs/blob/master/grd2-en-manual.pdf you can get the best Bash guide I ever read.

  • Related