I have a script as follows:

if [[ is_case1 -eq "1" ]]; then  

elif [[ is_case1 -eq "2" ]]; then   
   # initialize the variables here with specific logic 
  # initialize the variables here with specific logic 

I don’t really like this because if I have another case the initialization becomes longer and longer.
How can such cases be written more elegant?

closed as unclear what you're asking by Tomasz, Stephen Rauch, G-Man, DarkHeart, muru Nov 1 '17 at 7:18

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    you know bash allow switch statement ? – Archemar Oct 31 '17 at 9:00
  • @Archemar:Yes but how is a switch solving the core issue of too many paths? – Jim Oct 31 '17 at 9:13
  • 1
    What are the different cases and what changes between them? – Kusalananda Oct 31 '17 at 9:20
  • @Kusalananda:First case the variables are initialized from the script's params. The other cases the vars are initialized by assigning values within the if branch – Jim Oct 31 '17 at 9:34
  • Is there any reason why you do not use an array? – Kusalananda Oct 31 '17 at 9:36

If you use an array of values rather than individual variables, initialization my be prettier:


# set default values:
values=( "val1" "val2" "val3" )  # or values=()

case "$somevalue" in
    1) values=( "$@" ) ;;                    # get values from command line
    2) values=( "some" "other" "values" ) ;; # use other values
    *) # other cases uses default values
  • I like it but the only thing is that the values that you are using for initializing might not really exist. This is not really equivalent to the code I pasted since the array default would have actual values in case2. – Jim Oct 31 '17 at 11:34
  • @Jim It's not easy for me to guess what ... means in your code. – Kusalananda Oct 31 '17 at 11:35
  • @Jim Updated to do things slightly different. I still don't know what your requirements are. – Kusalananda Oct 31 '17 at 11:59
  • The only requirement is that the values in the vars for case 2 exist as valid values only if the condition holds true – Jim Oct 31 '17 at 12:55
  • it looks cleaner. The only change is that I need the values to be empty if it is none of the valid cases. So this values=( "val1" "val2" "val3" ) should be something like values=null? or not sure how to properly express it – Jim Oct 31 '17 at 17:15

Not sure what problem you're trying to solve exactly, but if you wanted to have the values of the several cases encoded in an associative array of arrays (for which you need ksh93, other shells don't support arrays of arrays), in ksh93 you could do something like:

#! /bin/ksh93

         [2]=(foo 'x y' bar baz)
  [whatever]=(w x y z)

values=(some default values)
[[ -v cases[$is_case1] ]] && values=("${cases[$is_case1][@]}")

# assign to separate variables if need be.
  • This is a very-very interesting approach! But I don't even know what is ksh93. Is there a way to do it that applies to bash? – Jim Nov 1 '17 at 8:12
  • @Jim, ksh93 is the shell bash copied most of its language and features from. It's a rewrite of ksh first released in 1993, while ksh was initially written in the early 80s and a subset of it (of ksh88) the basis for the POSIX sh specification (sh on most commercial Unices is based on ksh, sh on Solaris 11 is ksh93). – Stéphane Chazelas Nov 1 '17 at 9:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.