bash中有没有将字符串转换为小写字符串的方法?
例如,如果我有:
a="Hi all"
我想将其转换为:
"hi all"
bash中有没有将字符串转换为小写字符串的方法?
例如,如果我有:
a="Hi all"
我想将其转换为:
"hi all"
当前回答
在Bash 4中:
小写
$ string="A FEW WORDS"
$ echo "${string,}"
a FEW WORDS
$ echo "${string,,}"
a few words
$ echo "${string,,[AEIUO]}"
a FeW WoRDS
$ string="A Few Words"
$ declare -l string
$ string=$string; echo "$string"
a few words
大写
$ string="a few words"
$ echo "${string^}"
A few words
$ echo "${string^^}"
A FEW WORDS
$ echo "${string^^[aeiou]}"
A fEw wOrds
$ string="A Few Words"
$ declare -u string
$ string=$string; echo "$string"
A FEW WORDS
切换(未记录,但可在编译时配置)
$ string="A Few Words"
$ echo "${string~~}"
a fEW wORDS
$ string="A FEW WORDS"
$ echo "${string~}"
a FEW WORDS
$ string="a few words"
$ echo "${string~}"
A few words
大写(未记录,但可在编译时配置)
$ string="a few words"
$ declare -c string
$ string=$string
$ echo "$string"
A few words
标题大小写:
$ string="a few words"
$ string=($string)
$ string="${string[@]^}"
$ echo "$string"
A Few Words
$ declare -c string
$ string=(a few words)
$ echo "${string[@]}"
A Few Words
$ string="a FeW WOrdS"
$ string=${string,,}
$ string=${string~}
$ echo "$string"
A few words
要关闭声明属性,请使用+。例如,声明+c字符串。这会影响后续赋值,而不是当前值。
declare选项更改变量的属性,但不更改内容。示例中的重新分配会更新内容以显示更改。
编辑:
按照ghostdog74的建议,添加了“按单词切换第一个字符”(${var~})。
编辑:更正波浪号行为以匹配Bash 4.3。
其他回答
如果使用v4,则会进行烘焙。如果不是,这里有一个简单、广泛适用的解决方案。这个线程上的其他答案(和注释)对创建下面的代码非常有用。
# Like echo, but converts to lowercase
echolcase () {
tr [:upper:] [:lower:] <<< "${*}"
}
# Takes one arg by reference (var name) and makes it lowercase
lcase () {
eval "${1}"=\'$(echo ${!1//\'/"'\''"} | tr [:upper:] [:lower:] )\'
}
笔记:
执行:a=“Hi All”,然后:lcase a将执行与:a=$相同的操作(echolcase“Hi All”)在lcase函数中,使用${!1//\'/“'\'”}而不是${!1},即使字符串有引号,也可以使用此函数。
tr:
a="$(tr [A-Z] [a-z] <<< "$a")"
AWK:
{ print tolower($0) }
sed:
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
在bash 4中,可以使用类型集
例子:
A="HELLO WORLD"
typeset -l A=$A
这是JaredTS486方法的一个更快的变体,该方法使用本地Bash功能(包括Bash版本<4.0)来优化他的方法。
我已经为一个小字符串(25个字符)和一个大字符串(445个字符)计时了1000次这种方法的迭代,无论是小写还是大写转换。由于测试字符串主要是小写,所以转换为小写通常比转换为大写更快。
我已经将我的方法与本页上与Bash 3.2兼容的其他几个答案进行了比较。我的方法比这里记录的大多数方法性能更高,在某些情况下甚至比tr更快。
以下是25个字符的1000次迭代的计时结果:
0.46s表示我的小写方式;大写0.96s奥韦洛菲的小写方法为1.16秒;大写字母为1.59tr转小写为3.67s;大写3.81s11.12s用于ghostdog74的小写方法;大写31.41秒26.25秒用于技术龙的小写方式;大写字母为26.21sJaredTS486的小写方法为25.06s;大写27.04s
445个字符的1000次迭代的计时结果(包括威特·拜纳的诗歌《罗宾》):
我的小写方法是2s;大写字母为12s4s表示tr为小写;大写字母为4s奥韦洛菲(Orwellophile)的小写方法为20秒;大写字母为29sghostdog74的小写方式为75秒;669表示大写。值得注意的是,在比赛占优势的测试和失误占优势的考试之间,表现差异有多大467秒表示技术龙的小写方式;大写字母为449660秒用于JaredTS486的小写方法;大写为660s。有趣的是,这种方法在Bash中产生了连续的页面错误(内存交换)
解决方案:
#!/bin/bash
set -e
set -u
declare LCS="abcdefghijklmnopqrstuvwxyz"
declare UCS="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
function lcase()
{
local TARGET="${1-}"
local UCHAR=''
local UOFFSET=''
while [[ "${TARGET}" =~ ([A-Z]) ]]
do
UCHAR="${BASH_REMATCH[1]}"
UOFFSET="${UCS%%${UCHAR}*}"
TARGET="${TARGET//${UCHAR}/${LCS:${#UOFFSET}:1}}"
done
echo -n "${TARGET}"
}
function ucase()
{
local TARGET="${1-}"
local LCHAR=''
local LOFFSET=''
while [[ "${TARGET}" =~ ([a-z]) ]]
do
LCHAR="${BASH_REMATCH[1]}"
LOFFSET="${LCS%%${LCHAR}*}"
TARGET="${TARGET//${LCHAR}/${UCS:${#LOFFSET}:1}}"
done
echo -n "${TARGET}"
}
方法很简单:当输入字符串中有剩余的大写字母时,找到下一个,并用其小写变体替换该字母的所有实例。重复此操作,直到替换所有大写字母。
我的解决方案的一些性能特点:
仅使用shell内置实用程序,这避免了在新进程中调用外部二进制实用程序的开销避免子壳,这会导致性能损失使用针对性能进行编译和优化的shell机制,例如变量内的全局字符串替换、变量后缀修剪以及正则表达式搜索和匹配。这些机制比手动遍历字符串快得多仅循环要转换的唯一匹配字符计数所需的次数。例如,将具有三个不同大写字符的字符串转换为小写只需要3次循环迭代。对于预配置的ASCII字母表,循环迭代的最大次数为26UCS和LCS可以添加其他字符
对于Bash 3.2.+| Mac:
read -p 'What is your email? ' email
email=$(echo $email | tr '[:upper:]' '[:lower:]')
email="$email"
echo $email