Skip to main content
explaining how it actually works
Source Link
Gus Neves
  • 480
  • 6
  • 7

PleaseHow this works

The way this works is by getting the ASCII integer representation of each char with printf and then adding 32 if upper-to->lower, or subtracting 32 if lower-to->upper. Then use printf again to convert the number back to a char. From 'A' -to-> 'a' we have a difference of 32 chars.

Using printf to explain:

$ printf "%d\n" "'a"
97
$ printf "%d\n" "'A"
65

97 - 65 = 32

And this is the working version with examples.
Please note the comments in the code, as they explain the functionalitya lot of stuff:

For me it is fine, since I know I will only pass ASCII chars to it. I
I am using this for some case-insensitive CLI options, for example.

I am sure the code could be simplified a bit, having a mixed function for upper+lower chars for example. But his works fine and it is a lot easier to understand the internals like this.

Please note the comments in the code, as they explain the functionality:

For me it is fine, since I know I will only pass ASCII chars to it. I am using this for some case-insensitive CLI options, for example.

I am sure the code could be simplified a bit, having a mixed function for upper+lower chars for example. But his works fine and it is a lot easier to understand the internals like this.

How this works

The way this works is by getting the ASCII integer representation of each char with printf and then adding 32 if upper-to->lower, or subtracting 32 if lower-to->upper. Then use printf again to convert the number back to a char. From 'A' -to-> 'a' we have a difference of 32 chars.

Using printf to explain:

$ printf "%d\n" "'a"
97
$ printf "%d\n" "'A"
65

97 - 65 = 32

And this is the working version with examples.
Please note the comments in the code, as they explain a lot of stuff:

For me it is fine, since I know I will only pass ASCII chars to it.
I am using this for some case-insensitive CLI options, for example.

added 61 characters in body
Source Link
Gus Neves
  • 480
  • 6
  • 7

I am sure the code could be simplified a bit, having a mixed function for upper+lower chars for example. But his works fine and it is a lot easier to understand the internals like this.

I am sure the code could be simplified a bit, having a mixed function for upper+lower chars for example. But his works fine.

I am sure the code could be simplified a bit, having a mixed function for upper+lower chars for example. But his works fine and it is a lot easier to understand the internals like this.

ambiguous stuff
Source Link
Gus Neves
  • 480
  • 6
  • 7

If you know you will have Bash4 available you should really just use the ${VAR,,} notation (it is easy and cool). For Bash before 4 (My Mac still uses Bash 3.2 for example). I used the fixedcorrected version of @ghostdog74 's answer to create a more portable version.

If you know you will have Bash4 available you should really just use the ${VAR,,} notation (it is easy and cool). For Bash before 4 (My Mac still uses Bash 3.2 for example). I used the fixed version of @ghostdog74 's answer to create a more portable version.

If you know you will have Bash4 available you should really just use the ${VAR,,} notation (it is easy and cool). For Bash before 4 (My Mac still uses Bash 3.2 for example). I used the corrected version of @ghostdog74 's answer to create a more portable version.

Source Link
Gus Neves
  • 480
  • 6
  • 7
Loading