Search code examples
stringbashshelllowercase

How to convert a string to lower case in Bash, when the string is potentially -e, -E or -n?


In this question: How to convert a string to lower case in Bash?

The accepted answer is:

tr:

echo "$a" | tr '[:upper:]' '[:lower:]'

awk:

echo "$a" | awk '{print tolower($0)}'

Neither of these solutions work if $a is -e or -E or -n

Would this be a more appropriate solution:

echo "@$a" | sed 's/^@//' | tr '[:upper:]' '[:lower:]'

Solution

  • Use

    printf '%s\n' "$a" | tr '[:upper:]' '[:lower:]'