← Back to list

Case Transformation in Bash and POSIX with Examples

How to uppercase, lowercase, and capitalize

Shinichi Okada in mkdir Awesome · 2021-07-22 16:02 · 85 claps · 1.7 min read paywalled
#bash #posix #case-transformation #case-conversion #uppercase
Open on Medium ↗

Case Transformation in Bash and POSIX with Examples

How to uppercase, lowercase, and capitalize

Photo by Alexander Andrews on Unsplash

Photo by Alexander Andrews on Unsplash

Bash

Use ^ to convert the first letter to uppercase and ^^ to convert all characters to uppercase.

Use , to convert the first letter to lowercase and ,, to convert all characters to lowercase.

Use ~ to toggles the case for the first character and ~~ to toggle cases for all characters.

Try the following examples.

[embed]

In Bash, you can do it in a different way:

[embed]

You can use converting the first letter with the read command to get the user’s response:

[embed]Converting the first letter to uppercase after a read command. Code example by the author.

In line 12: <<< denotes a here-string. It passes the word on the right to the standard input of the command on the left, in this case, it is the tr command.

We use ${parameter:offset:length} to get a substring from a variable. It expands to up to length characters of the value of parameter starting at the character specified by offset. In our case, it is the first letter.

We transform the first letter from lowercase to uppercase and concatenate it to the rest of the letters ${VAR_3:1}. If we don’t specify the length, it extends to the end of the value.

Please note that << denotes a here document that takes multiple lines.

[embed]

POSIX

If you are looking for portability, use the [tr command with [:lower:] and [:upper:]](https://tech.io/snippet/JCFhOEk). The [tr](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/tr.html#tag_20_132) command translates characters by using it with character classes. You can use a character class using the syntax [:class:] where class is one of the POSIX character classes.

# POSIX character classes
alnum: Alphanumeric characters [:alpha:] and [:digit:]
blank: Space and tag 
digit: 0-9 
lower: Lowercase letters
punct: Punctuation characters
upper: Uppercase letters
alpha: Alphabetic characters [:lower:] and [:upper:]
cntrl: Control characters
graph: Graphic characters [:alnum:] and [:punct:]
print: Printable characters
space: Space characters
xdigit: Hexadecimal digits 0-9 A-F a-f

[embed]

Transforming array elements

You can apply these rules to arrays as well.

[embed]

Use array[@] with ^, ^^, ,, ,,, ~, ~~ to convert array elements as described before.

Get full access to every story on Medium by becoming a member.

https://blog.codewithshin.com/subscribe

https://blog.codewithshin.com/subscribe

References


메타데이터
post_id
acdc1e0d0bc4
slug
case-transformation-in-bash-and-posix-with-examples-acdc1e0d0bc4
url
https://medium.com/mkdir-awesome/case-transformation-in-bash-and-posix-with-examples-acdc1e0d0bc4
canonical_url
https://medium.com/mkdir-awesome/case-transformation-in-bash-and-posix-with-examples-acdc1e0d0bc4
author_url
https://medium.com/@shinichiokada
status
ok
fetched_at
2026-07-16 07:17:41