With the zsh
shell:
set -o extendedglob
lc_var=${var/%(#m) */${(L)MATCH}}
Where ${var/%pattern/replacement}
like in ksh replaces the end of the var that matches the pattern
with the replacement
, (#m)
causes the matched portion to be stored in $MATCH
(that's the part that needs extendedglob
) and ${(L)MATCH}
converts $MATCH
to lowercase.
With the bash
shell:
tmp1=${var%% *}
tmp2=${var#"$tmp1"}
lc_var=$tmp1${tmp2,,}
POSIXly, you'd do:
lc_var=$(
awk '
BEGIN{
var = ARGV[1]
if (i = index(var, " "))
var = substr(var, 1, i) tolower(substr(var, i + 1))
print var "."
}' "$var"
)
lc_var=${lc_var%.}
In any case, that can't look like lc_var=$(echo $var ...)
because $(...)
is POSIX/Korn/bash/zsh shell syntax, and in those shells, echo
can't output arbitrary data and variable expansions should be quoted (except maybe in zsh
).
All those variants turn to lower case all the characters past the first space character in $var
(making no assumption on what characters they may be). You'd need to adapt it if words are possibly delimited by characters other than space, or if there may be word delimiter characters before the first word.
awk
doesn't process any input when there are onlyBEGIN
statements (and optional function definitions). – Stéphane Chazelas Jun 10 '19 at 07:11sed
can do it with\L
as you've shown (that syntax coming fromvi
/ex
initially IIRC), POSIXsed
can do it withy
but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...) – Stéphane Chazelas Jun 10 '19 at 07:31sed
, there'secho | VAR=$var@ sed 's/^/printenv VAR/e; s/@$//; ...'
(on systems without aprintenv
command, you can replace withecho | VAR=$var 's/^/printf "%s@" "$VAR"/e; s/@$//;...'
. POSIXly:printf '%s\n' "$var" | sed -e :1 -e '$!{N;b1' -e '}' -e ...
– Stéphane Chazelas Jun 10 '19 at 08:31