syntax:ccmd:case

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
syntax:ccmd:case [2013/02/01 13:27]
ormaaj [Examples] A practical example to show off my video power function.
syntax:ccmd:case [2013/04/14 12:33]
thebonsai Don't tread version 4 special - it has been around for a long time now
Line 15: Line 15:
 The ''​case''​-statement can execute commands based on a [[syntax:​pattern | pattern matching]] decision. The word ''<​WORD>''​ is matched against every pattern ''<​PATTERNn>''​ and on a match, the associated [[syntax:​basicgrammar#​lists | list]] ''<​LISTn>''​ is executed. Every commandlist is terminated by ''<​nowiki>;;</​nowiki>'',​ this rule is optional for the very last commandlist (i.e. you can omit the ''<​nowiki>;;</​nowiki>''​ before the ''​esac''​). The ''​case''​-statement can execute commands based on a [[syntax:​pattern | pattern matching]] decision. The word ''<​WORD>''​ is matched against every pattern ''<​PATTERNn>''​ and on a match, the associated [[syntax:​basicgrammar#​lists | list]] ''<​LISTn>''​ is executed. Every commandlist is terminated by ''<​nowiki>;;</​nowiki>'',​ this rule is optional for the very last commandlist (i.e. you can omit the ''<​nowiki>;;</​nowiki>''​ before the ''​esac''​).
  
-:V4: Bash 4 introduces two new action terminators. The classic behavior using '';;''​ is to execute only the list associated with the first matching pattern, then break out of the ''​case''​ block. The '';&''​ terminator causes ''​case''​ to also execute the next block without testing its pattern. The '';;&''​ operator is like '';;'',​ except the case statement doesn'​t terminate after executing the associated list - Bash just continues testing the next pattern as though the previous pattern didn't match. Using these terminators,​ a ''​case''​ statement can be configured to test against all patterns, or to share code between blocks, for example.+Bash 4 introduces two new action terminators. The classic behavior using '';;''​ is to execute only the list associated with the first matching pattern, then break out of the ''​case''​ block. The '';&''​ terminator causes ''​case''​ to also execute the next block without testing its pattern. The '';;&''​ operator is like '';;'',​ except the case statement doesn'​t terminate after executing the associated list - Bash just continues testing the next pattern as though the previous pattern didn't match. Using these terminators,​ a ''​case''​ statement can be configured to test against all patterns, or to share code between blocks, for example.
  
 The word ''<​WORD>''​ is expanded using //tilde//, //​parameter//​ and //variable expansion//;​ //​arithmetic//,​ //command// and //process substitution//;​ and //quote removal//. **No word splitting, brace, or pathname expansion is done**, which means you can leave expansions unquoted without problems: The word ''<​WORD>''​ is expanded using //tilde//, //​parameter//​ and //variable expansion//;​ //​arithmetic//,​ //command// and //process substitution//;​ and //quote removal//. **No word splitting, brace, or pathname expansion is done**, which means you can leave expansions unquoted without problems:
Line 27: Line 27:
 This is similar to the behavior of the [[syntax:​ccmd:​conditional_expression | conditional expression command ("new test command"​)]] (also no word splitting for expansions). This is similar to the behavior of the [[syntax:​ccmd:​conditional_expression | conditional expression command ("new test command"​)]] (also no word splitting for expansions).
  
-Unlike the C-case-statement,​ only the matching list and nothing else is executed. If more patterns match the word, only the first match is taken. (**Note** the comment about :V4: Bash v4 changes above.)+Unlike the C-case-statement,​ only the matching list and nothing else is executed. If more patterns match the word, only the first match is taken. (**Note** the comment about Bash v4 changes above.)
  
 Multiple ''​|''​-delimited patterns can be specified for a single block. This is a POSIX-compatable equivalent to the ''​@(pattern-list)''​ extglob construct. Multiple ''​|''​-delimited patterns can be specified for a single block. This is a POSIX-compatable equivalent to the ''​@(pattern-list)''​ extglob construct.
  • syntax/ccmd/case.txt
  • Last modified: 2017/02/08 15:04
  • by fgrose