我想用bash将字符串中的第一个字符大写。

foo="bar";

//uppercase first character

echo $foo;

应打印“Bar”;


当前回答

下面是“原生”文本工具的使用方法:

#!/bin/bash

string="abcd"
first=`echo $string|cut -c1|tr [a-z] [A-Z]`
second=`echo $string|cut -c2-`
echo $first$second

其他回答

仅使用awk

foo="uNcapItalizedstrIng"
echo $foo | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}'
$ foo="bar";
$ foo=`echo ${foo:0:1} | tr  '[a-z]' '[A-Z]'`${foo:1}
$ echo $foo
Bar

据我所知,这是POSIX sh兼容的。

upper_first.sh:

#!/bin/sh

printf "$1" | cut -c1 -z | tr -d '\0' | tr [:lower:] [:upper:]
printf "$1" | cut -c2-

Cut -c1 -z以\0而不是\n结束第一个字符串。它被tr -d '\0'删除。它也可以省略-z并使用tr -d '\n'来代替,但如果字符串的第一个字符是换行符,则会中断。

用法:

$ upper_first.sh foo
Foo
$

在函数中:

#!/bin/sh

function upper_first ()
{
    printf "$1" | cut -c1 -z | tr -d '\0' | tr [:lower:] [:upper:]
    printf "$1" | cut -c2-
}

old="foo"
new="$(upper_first "$old")"
echo "$new"
foo="$(tr '[:lower:]' '[:upper:]' <<< ${foo:0:1})${foo:1}"

它也可以在纯bash中使用bash-3.2完成:

# First, get the first character.
fl=${foo:0:1}

# Safety check: it must be a letter :).
if [[ ${fl} == [a-z] ]]; then
    # Now, obtain its octal value using printf (builtin).
    ord=$(printf '%o' "'${fl}")

    # Fun fact: [a-z] maps onto 0141..0172. [A-Z] is 0101..0132.
    # We can use decimal '- 40' to get the expected result!
    ord=$(( ord - 40 ))

    # Finally, map the new value back to a character.
    fl=$(printf '%b' '\'${ord})
fi

echo "${fl}${foo:1}"