1524 votes

La conversion d'une chaîne en minuscules dans le shell Bash scripting

Est-il possible de Bash shell script, de sorte que je peux convertir une chaîne de caractères en minuscules chaîne?

Par exemple,

if $a = "Hi all"

Je veux le convertir en

$a = "hi all"

2601voto

ghostdog74 Points 86060

Les différentes manières:

tr

$ echo $a | tr '[:upper:]' '[:lower:]'
hi all

AWK

$ echo $a | awk '{print tolower($0)}'
hi all

Bash 4.0

$ echo ${a,,}
hi all

Perl

$ echo $a | perl -ne 'print lc'
hi all

Bash

lc(){
    case "$1" in
        [A-Z])
        n=$(printf "%d" "'$1")
        n=$((n+32))
        printf \\$(printf "%o" $n)
    esac
}
word="ABX"
for((i=0;i<${#word};i++))
do
    ch=${word:$i:1}
    lc $ch
done

495voto

Dennis Williamson Points 105818

En Bash 4:

Minuscules

$ 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

Pour les majuscules

$ 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

Bascule (sans-papiers)

$ 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

Capitalize (sans-papiers)

$ string="a few words"
$ declare -c string
$ string=$string
$ echo $string
A few words

Titre:

$ 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

Pour désactiver un declare d'attribut, utilisez +. Par exemple, declare +c string. Cela affecte les affectations ultérieures et non pas de la valeur.

Edit:

Ajouté à "changer de premier caractère d'un mot" (${var~}) comme suggéré par ghostdog74.

135voto

shuvalov Points 1609
echo "Hi All" | tr "[:upper:]" "[:lower:]"

93voto

tr:

a="$(tr [A-Z] [a-z] <<< "$a")"

AWK:

{ print tolower($0) }

sed:

y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/

67voto

nettux443 Points 927

Je sais que c'est un peu vieux post, mais j'ai fait cette réponse pour un autre site, donc je pensais que je poste ici:

HAUT -> bas: l'utilisation de python:

b=`echo "print '$a'.lower()" | python`

Ou Ruby:

b=`echo "print '$a'.downcase" | ruby`

Ou Perl (probablement mon préféré):

b=`perl -e "print lc('$a');"`

Ou PHP:

b=`php -r "print strtolower('$a');"`

Ou Awk:

b=`echo "$a" | awk '{ print tolower($1) }'`

Ou Sed:

b=`echo "$a" | sed 's/./\L&/g'`

Ou Bash 4:

b=${a,,}

Ou NodeJS si vous en avez un (et un peu de noix...):

b=`echo "console.log('$a'.toLowerCase());" | node`

Vous pouvez également utiliser dd (mais je ne le ferais pas!):

b=`echo "$a" | dd  conv=lcase 2> /dev/null`

bas -> HAUT:

l'utilisation de python:

b=`echo "print '$a'.upper()" | python`

Ou Ruby:

b=`echo "print '$a'.upcase" | ruby`

Ou Perl (probablement mon préféré):

b=`perl -e "print uc('$a');"`

Ou PHP:

b=`php -r "print strtoupper('$a');"`

Ou Awk:

b=`echo "$a" | awk '{ print toupper($1) }'`

Ou Sed:

b=`echo "$a" | sed 's/./\U&/g'`

Ou Bash 4:

b=${a^^}

Ou NodeJS si vous en avez un (et un peu de noix...):

b=`echo "console.log('$a'.toUpperCase());" | node`

Vous pouvez également utiliser dd (mais je ne le ferais pas!):

b=`echo "$a" | dd  conv=ucase 2> /dev/null`

Aussi, quand vous dites "shell" je suis en supposant que vous entendez bash mais si vous pouvez utiliser zsh c'est aussi simple que

b=$a:l

pour les minuscules et

b=$a:u

pour les majuscules.

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X