shell之大小写转换

1. 小写转大写

1. 1 全部转换成大写

${str^^}

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *;do echo ${name^^} ;done
00-RELEASENOTES
BUGS
CONTRIBUTING
COPYING
INSTALL
MANIFESTO
MAKEFILE
README.MD
TLS.MD
DEPS
REDIS.CONF
RUNTEST
RUNTEST-CLUSTER
RUNTEST-MODULEAPI
RUNTEST-SENTINEL
SENTINEL.CONF
SRC
TESTS
UTILS

echo str | tr 'a-z' 'A-Z'

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *;do echo $name | tr 'a-z' 'A-Z' ;done
00-RELEASENOTES
BUGS
CONTRIBUTING
COPYING
INSTALL
MANIFESTO
MAKEFILE
README.MD
TLS.MD
DEPS
REDIS.CONF
RUNTEST
RUNTEST-CLUSTER
RUNTEST-MODULEAPI
RUNTEST-SENTINEL
SENTINEL.CONF
SRC
TESTS
UTILS

declare -u 变量

bash4 或更高版本
通过声明变量类型,赋值时自动转换

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ declare -u UPV;for name in *;do UPV=$name;echo $UPV;done
00-RELEASENOTES
BUGS
CONTRIBUTING
COPYING
INSTALL
MANIFESTO
MAKEFILE
README.MD
TLS.MD
DEPS
REDIS.CONF
RUNTEST
RUNTEST-CLUSTER
RUNTEST-MODULEAPI
RUNTEST-SENTINEL
SENTINEL.CONF
SRC
TESTS
UTILS
$ 

1.2 首字符转换成大写

${str^}

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *;do echo ${name^} ;done
00-RELEASENOTES
BUGS
CONTRIBUTING
COPYING
INSTALL
MANIFESTO
Makefile
README.md
TLS.md
Deps
Redis.conf
Runtest
Runtest-cluster
Runtest-moduleapi
Runtest-sentinel
Sentinel.conf
Src
Tests
Utils

1.3 仅首字符大写

declare -c 变量

bash4 或更高版本
通过声明变量类型,赋值时自动转换

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ declare -c Upv;for name in *; do Upv=$name;echo $Upv;done
00-releasenotes
Bugs
Contributing
Copying
Install
Manifesto
Makefile
Readme.md
Tls.md
Deps
Redis.conf
Runtest
Runtest-cluster
Runtest-moduleapi
Runtest-sentinel
Sentinel.conf
Src
Tests
Utils

2. 大写转小写

2. 1 全部转换成小写

${str,,}

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *;do echo ${name,,};done
00-releasenotes
bugs
contributing
copying
install
manifesto
makefile
readme.md
tls.md
deps
redis.conf
runtest
runtest-cluster
runtest-moduleapi
runtest-sentinel
sentinel.conf
src
tests
utils

echo str | tr 'A-Z' 'a-z'

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *; do echo $name | tr 'A-Z' 'a-z'; done
00-releasenotes
bugs
contributing
copying
install
manifesto
makefile
readme.md
tls.md
deps
redis.conf
runtest
runtest-cluster
runtest-moduleapi
runtest-sentinel
sentinel.conf
src
tests
utils

declear -l 变量

bash4 或更高版本
通过声明变量类型,赋值时自动转换

$ bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ declare -l lowv;for name in *; do lowv=$name;echo $lowv; done
00-releasenotes
bugs
contributing
copying
install
manifesto
makefile
readme.md
tls.md
deps
redis.conf
runtest
runtest-cluster
runtest-moduleapi
runtest-sentinel
sentinel.conf
src
tests
utils

2.2 首字符转换成小写

${str,}

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *; do echo ${name,}; done
00-RELEASENOTES
bUGS
cONTRIBUTING
cOPYING
iNSTALL
mANIFESTO
makefile
rEADME.md
tLS.md
deps
redis.conf
runtest
runtest-cluster
runtest-moduleapi
runtest-sentinel
sentinel.conf
src
tests
utils

3. 大小写互转

3.1 全部逆转

${str~~}

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *; do echo ${name~~};done
00-releasenotes
bugs
contributing
copying
install
manifesto
mAKEFILE
readme.MD
tls.MD
DEPS
REDIS.CONF
RUNTEST
RUNTEST-CLUSTER
RUNTEST-MODULEAPI
RUNTEST-SENTINEL
SENTINEL.CONF
SRC
TESTS
UTILS

3.2 首字符逆转

${str~}

$ ls
00-RELEASENOTES  COPYING    Makefile   deps        runtest-cluster    sentinel.conf  utils
BUGS             INSTALL    README.md  redis.conf  runtest-moduleapi  src
CONTRIBUTING     MANIFESTO  TLS.md     runtest     runtest-sentinel   tests

$ for name in *; do echo ${name~};done
00-RELEASENOTES
bUGS
cONTRIBUTING
cOPYING
iNSTALL
mANIFESTO
makefile
rEADME.md
tLS.md
Deps
Redis.conf
Runtest
Runtest-cluster
Runtest-moduleapi
Runtest-sentinel
Sentinel.conf
Src
Tests
Utils
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值