If the string is already stored in a variable you can use bash
's parameter expansion, specifially ${parameter,,pattern}
(available since bash 4.0), where parameter
is the name of your variable and pattern
is ommitted:
$ string="Hello, World!"
$ echo $string
Hello, World!
$ echo ${string,,}
hello, world!
Note that this does not change the value of the variable, only the output. To change the variable you have to assign the new value:
$ echo $string
Hello, World!
$ string=${string,,}
$ echo $string
hello, world!
The upper-case conversion works with ${parameter^^pattern}
:
$ echo ${string^^}
HELLO, WORLD!
This works also with Unicode strings (at least with current bash versions, probably needs at least bash 4.3):
$ string='ἈΛΦΆβητος'
$ echo ${string,,}
ἀλφάβητος
$ echo ${string^^}
ἈΛΦΆΒΗΤΟΣ
If you are using zsh
, you can use Parameter Expansion Flags (${(FLAGS)NAME}
; available since zsh 2.5) to achieve the same results. The bash
syntax does not work in zsh
1). The flag for lower case is L
; for upper case it is U
:
$ string="Hello, World!"
$ echo ${(L)string}
hello, world!
$ echo ${(U)string}
HELLO, WORLD!
$ echo $string
Hello, World!"
This also works with Unicode strings (at least since zsh 5.0; I did not try with earlier versions):
$ string='ἈΛΦΆβητος'
$ echo ${(L)string}
ἀλφάβητος
$ echo ${(U)string}
ἈΛΦΆΒΗΤΟΣ
1) Although, seeing that zsh had this for far longer, it should probably be: "the zsh
syntax does not work in bash
.
You cannot express number ranges easily in the case
expressions - the pattern [1000-9999]
, for example, does not mean the numbers 1000
to 9999
, but the characters 1
, 0
, 0
, the range 0-9
, the characters 9
, 9
, 9
- essentially all the digits. [1-85]
does not mean the numbers 1
to 85
, but the digits from 1
to 8
, and 5
, ... which are just the digits from 1
to 8
. So [1-20]*
means anything that begins with 1
, 2
, or 0
- so even 20000000
will match that. Use if
/then
/elif
/else
/fi
instead:
if (( $SECURITY_PACKAGES == 0 ))
then
echo "OK - not bad: There are a total of $TOTAL_PACKAGES packages to upgrade in this server, but none of them are security updates!"
exit 0
elif (( $SECURITY_PACKAGES <= 20 ))
then
echo "WARNING - $TOTAL_PACKAGES packages required to upgrade in this server, of which $SECURITY_PACKAGES are security updates"
exit 1
elif (( $SECURITY_PACKAGES <= 9999 ))
then
echo "CRITICAL - $SECURITY_PACKAGES out of $TOTAL_PACKAGES are security updates! Consider upgrading soon!"
exit 2
else
echo "UNKNOWN - I am not sure what's happening now, check later or check server: $TOTAL_PACKAGES to upgrade, $SECURITY_PACKAGES are security updates"
exit 3
fi
Best Answer
I'd suspect some error in
~/.inputrc
or/etc/inputrc
. Those file are read even if you call Bash with--norc
.