help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bash “case” statement


From: Greg Wooledge
Subject: Re: Bash “case” statement
Date: Wed, 12 Jul 2023 19:57:04 -0400

On Wed, Jul 12, 2023 at 06:52:32PM -0500, Tony Esposito wrote:
> Is the bash “case”
> statement just syntactic sugar?

Everything is, if you want to define it that way.

> That is, does the bash interpreter convert the “case” statement into an
> equivalent “if-then-else” statement and then do the interpretation?

Internally?  Does it really matter?

I don't know or care what bash does internally after parsing the commands.
In the POSIX shell, the 'case' statement has the unique power of being
able to match data against globs.

case $file in
    *.txt) process-as-text "$file;;
    *.png) ...;;
esac

That's not possible any other way in sh, without using external programs.
But bash has [[ ]] which can also match strings against globs, so in
theory you could also write it like this:

if [[ $file = *.txt ]]; then
    process-as-text "$file"
elif [[ $file = *.png ]]; then
    ..
fi

The case statement is nicer-looking.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]