151
140

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Bashで変数を大文字小文字変換(uppercase/lowercase)する

Last updated at Posted at 2013-04-23

bashのバージョン4以降から使えるようになった構文なんだけど、大文字小文字無視した文字列で比較したい時とかに地味に便利なのでメモ。

記述 説明
${v^} 大文字化(1文字目のみ) v="ho ge"; echo ${v^}→Ho ge
${v^^} 大文字化(全文字) v="ho ge"; echo ${v^^}→HO GE
${v,} 小文字化(1文字目のみ) v="HO GE"; echo ${v,}→hO GE
${v,,} 小文字化(全文字) v="HO GE"; echo ${v,,}→ho ge
${v~} 大小反転(1文字目のみ) v="ho GE"; echo ${v~}→Ho ge
${v~~} 大小反転(全文字) v="ho GE"; echo ${v~~}→HO ge

利用例

#!/bin/bash
echo -n "押すなよ!絶対押すなよ! [y/N]"
read confirm
if [[ ${confirm,,} = y ]]; then
  echo "素直か!"
else
  echo "どーん!"
fi

bashが古い場合は…

trでも使っとけばいいんじゃね。

$ tr '[a-z]' '[A-Z]' <<<hoge
HOGE
151
140
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
151
140

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?