0

I want to generate an output in a bash script that is dependent on a previous input.

The input is stored in the variable OPTION. It can be 1 or 2.

After the input a text should follow:

oname = ${OPTION} == 1 ? "OPTION ONE" : "OPTION TWO";
echo "Your selection was: ${oname}" ?

But I got this error: line xx: ==: comman not found.

What am I doing wrong?

Maik Lowrey
  • 15,957
  • 6
  • 40
  • 79
  • 3
    The ternary is only for arithmetic. It's unavailable in any other context; you can do `oname=$(( option == 1 ? 2 : 3 ))`, but that's basically the _only_ thing it's good for. – Charles Duffy Oct 05 '22 at 19:11
  • @CharlesDuffy Ok. Thank you for info. is there an alternative or do i have to build a classic if else condition for this? – Maik Lowrey Oct 05 '22 at 19:14
  • 1
    The linked duplicate shows you some options, like `case` statements. In a lot of circumstances an associative array (bash's version of what some other languages call "dicts" or "maps") might be appropriate too, with keys for the valid option names. – Charles Duffy Oct 05 '22 at 19:15
  • 1
    If for example you actually had three options and a default, you might have `options=( [1]="OPTION ONE" [2]="OPTION TWO" [3]="OPTION 3" )`, so you could check whether `[[ ${options[$OPTION]} ]]` is true, use that value if it's nonempty, or use your default otherwise. – Charles Duffy Oct 05 '22 at 19:16
  • @CharlesDuffy All right and thank you very much! I had already thought about an array and that is what I will do. Greetings! – Maik Lowrey Oct 05 '22 at 19:17
  • 1
    This can be an option : `test "${OPTION}" = 1 && oname="OPTION ONE" || oname="OPTION TWO"` – Philippe Oct 05 '22 at 20:25

0 Answers0