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
.
First, you need to remove the text from around the assignment of the string variable:
text="'xdc','cde','erd','ded','ded','kie';"
Then you can just use the array form of the bash read
command:
IFS=, read -a ids <<< "${text%;}"
where the ${text%;}
substitution removes the trailing semicolon. Note that, this way, the IFS is not modified outside of the read
command so there's no need to save and restore it.
Your C-style for-loop syntax is almost correct, except that in bash, the loop needs double parentheses e.g.
for ((i=0; i<${#ids[@]}; ++i)); do printf '%s\n' "${ids[i]}"; done
Alternatively, you can loop over array members directly using a for ... in
loop:
for i in "${ids[@]}"; do printf '%s\n' "$i"; done
Best Answer
I think you're looking for this,
It changes the seconds since 1970-01-01 00:00:00 UTC to a human-readable string.
Example:
source