Questions tagged [case]

A reserved word in shells used for running commands based on pattern matching. It must be terminated by "esac".

References

88 questions
89
votes
4 answers

What does "esac" mean at the end of a bash case statement? Is it required?

I have found multiple examples of "esac" appearing at the end of a bash case statement but I have not found any clear documentation on it's use. The man page uses it, and even has an index on the word…
GrnMtnBuckeye
  • 1,017
  • 1
  • 7
  • 6
56
votes
3 answers

Possible to match multiple conditions in one case statement?

I would like to do something like this where on Friday, the output is for both conditions that match: #!/bin/bash #!/bin/bash NOW=$(date +"%a") case $NOW in Mon) echo "Mon";; Tue|Wed|Thu|Fri) echo "Tue|Wed|Thu|Fri";; …
MountainX
  • 17,168
  • 59
  • 155
  • 264
25
votes
7 answers

How can I use a variable as a case condition?

I am trying to use a variable consisting of different strings separated with a | as a case statement test. For example: string="\"foo\"|\"bar\"" read choice case $choice in $string) echo "You chose $choice";; *) echo "Bad…
terdon
  • 234,489
  • 66
  • 447
  • 667
13
votes
8 answers

Case fallthrough based on if condition

I am looking for a way to have fallthrough happen based on an if condition within a case condition in bash. For example: input="foo" VAR="1" case $input in foo) if [ $VAR = "1" ]; then # perform fallthrough else # do not…
Smashgen
  • 383
  • 1
  • 5
  • 8
11
votes
2 answers

Variable assignment outside of case statement

In many languages it is possible to assign the result of a case/switch statement to a variable, rather than repeating the variable assignment many times within the case statement. Is it possible to do something like this in the Bash…
iconoclast
  • 9,057
  • 12
  • 56
  • 95
10
votes
2 answers

POSIX catch newline in case statement

I want to catch if a variable is multiline in a case statement in POSIX shell (dash). I tried this: q=' ' case "$q" in *$'\n'*) echo nl;; *) echo NO nl;; esac It returns nl in zsh but NO nl in dash. Thanks.
aaa
  • 207
  • 1
  • 11
9
votes
1 answer

Using a variable as a case condition in zsh

My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example: input="foo" pattern="(foo|bar)" case $input…
Smashgen
  • 383
  • 1
  • 5
  • 8
8
votes
4 answers

How to re-run the case statement if the input is invalid?

I have the following code in the middle of a script to confirm whether we want to resume the script or not. read -r -p "Would you like to continue [Y/N] : " i case $i in [yY]) echo -e "Resuming the script";; [nN]) …
user308897
7
votes
2 answers

How do I differentiate between uppercase and lowercase characters in a case statement?

I'm trying to revive my rusty shell scripting skills, and I've run into a problem with case statements. My goal in the program below is to evaluate whether a user-supplied string begins with a capital or lowercase letter: # practicing case…
John Jones
  • 73
  • 1
  • 7
7
votes
2 answers

Using a variable as pattern for case command

So this is my code: #!/bin/bash action_list='list|add|rem' while true; do echo "Actions include: list - show list add - add item to list rem - remove item from list" …
user361323
  • 81
  • 2
6
votes
3 answers

AND operator in case statement

I have following code. read -p "Enter a word: " word case $word in [aeiou]* | [AEIOU]*) echo "The word begins with a vowel." ;; [0-9]*) echo "The word begins with a digit." ;; *[0-9]) echo "The word ends with a…
smc
  • 561
  • 3
  • 10
  • 24
5
votes
3 answers

Case on multiple variables at once bash

I want to know if there is any way to case on multiple variables at once, like this: #/bin/bash arr1=(1 2) arr2=(3 4) foo=1 bar=2 case $foo && $bar in ${arr1[@]}) echo "variables equal array 1!";; ${arr2[@]}) echo "variables…
5
votes
2 answers

How to match a specific word or its parts in a case statement?

Suppose one has the following case: #!/bin/sh case $1 in e|ex|exa|exam|examp|exampl|example) echo "OK" ;; t|te|tes|test) echo "Also OK" ;; *) echo "Error!" ;; esac Is there a more elegant and at the same time POSIX-compliant solution (i.e., no…
user464484
  • 53
  • 4
5
votes
4 answers

Using case and arrays together in bash

Is it possible to check if a variable is contained inside an array using case? I would like to do something like ARR=( opt1 opt2 opt3 ); case $1 in $ARR) echo "Option is contained in the array"; *) echo "Option is not…
red_trumpet
  • 335
  • 1
  • 3
  • 11
4
votes
1 answer

how to use "joker" or wildcard in string patterns (spaces separated words) in a bash case statement?

How to use bash's "case" statement for "very specific" string patterns (multiple words, including spaces) ? The problem is: I receive a multi-word String from a function which is pretty specific, including a version number. The version number now…
Axel Werner
  • 978
  • 1
  • 9
  • 19
1
2 3 4 5 6