I am comparing string in case statement as below : input variable can be D P KL ...

case $input in
          val="Hi" ;;
         val="Bye" ;;
         echo $input

input variable not printing anything..

  • 3
    What is the value in $input? Also, the case statement is partial (not closed by esac), and you're not using $val anywhere. – Kusalananda Mar 29 '18 at 7:39
  • Should always be case "$input". It seems to me that the esac is missing. Or shall $input be printed only in the last case? And why $input and not $val? Strange code... – Hauke Laging Mar 29 '18 at 7:42
  • 1
    @HaukeLaging Quoting $input in the case statement is good practice, but actually not necessary. unix.stackexchange.com/questions/68694/… (note, I always quote the expansion in case statements) – Kusalananda Mar 29 '18 at 7:51
  • yeah ..it was by mistake i didnot esac – TKHN Mar 29 '18 at 8:24

There are two main issues in you script:

  1. The case statement is not closed by esac.
  2. The third pattern contains || which is a syntax error in most Bourne-like shells (use '' or "" or an expansion that resolves to an empty value to match on the empty string portably)

It's unclear what your script is actually doing, so I'm speculating a bit and wrote this:



case "$input" in
          val='Hello' ;;
          val='Hi' ;;
          val='Bye' ;;
          echo 'error' >&2
          exit 1

printf 'input was "%s", val is "%s"\n' "$input" "$val"

Testing it:

$ ./script.sh D
input was "D", val is "Hello"

$ ./script.sh MM
input was "MM", val is "Bye"

$ ./script.sh BOO
  • thanks it works when used like the above way..but not working in while loop – TKHN Mar 29 '18 at 8:26
  • i have accepted this answer...shall i ask here only or a new question? – TKHN Mar 29 '18 at 8:26
  • @TKHN Since you have accepted my answer (thanks!) and since the question does not mention a while loop, I think it's best to ask a completely new question. Make sure to not only show the code, but also what the input and expected output is, and any error messages that you get. – Kusalananda Mar 29 '18 at 8:31
  • unix.stackexchange.com/questions/434262/… ..could u please check – TKHN Mar 29 '18 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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