Search code examples
regexshelltext-processingcamelcasingsnakecasing

camelCase to snake_case using the shell?


What's a simple way to convert (a stream of space-separated) words in camelCase to snake_case, using the command shell (e.g. bash)?

This is a simpler case relative to an earlier question which I've deleted.


Solution

  • With GNU sed you can easily convert to lowercase and prefix with underscore:

    $ echo camelCase andAnother | sed 's/[A-Z]/_\l&/g'
    camel_case and_another