Convert only certain words to lowercaseWhy is printf better than echo?Why does my shell script choke on whitespace or other special characters?Find a string in one section of a file with mutiple sectionsHow to replace the content of specific column with awk? Tab Delimited FileIssues with storing an echo of a date conversion into a string variable inunixprint only lines where the first column is uniqueUse sed to retrieve variable from 1 file and place it in anotherHow to find/grep what is between string1 and string2?sed use from android shellusing awk with variablesHow to use sed to replace a linux path in a specific text fileHow to convert multiline to singleline but preserve paragraphs

If the mass of the Earth is decreasing by sending debris in space, does its angular momentum also decrease?

How did the European Union reach the figure of 3% as a maximum allowed deficit?

I'm yearning in grey

Build a scale without computer

What is this airplane that sits in front of Barringer High School in Newark, NJ?

Why swap space doesn't get filesystem check at boot time?

Expand command in an argument before the main command

How do I become a better writer when I hate reading?

Why we can't jump without bending our knees?

Justifying Affordable Bespoke Spaceships

Harmonic Series Phase Difference?

Is a sequel allowed to start before the end of the first book?

What is "dot" sign in •NO?

How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?

Probability Dilemma

What are the mechanical differences between Adapt and Monstrosity?

Do battery electrons only move if there is a positive terminal at the end of the wire?

What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?

Leaving job close to major deadlines

Checking if argument is a floating point without breaking on control sequences in argument

Is swap gate equivalent to just exchanging the wire of the two qubits?

Does cooling a potato change the nature of its carbohydrates?

How do I correctly reduce geometry on part of a mesh?

Scaling an object to change its key



Convert only certain words to lowercase


Why is printf better than echo?Why does my shell script choke on whitespace or other special characters?Find a string in one section of a file with mutiple sectionsHow to replace the content of specific column with awk? Tab Delimited FileIssues with storing an echo of a date conversion into a string variable inunixprint only lines where the first column is uniqueUse sed to retrieve variable from 1 file and place it in anotherHow to find/grep what is between string1 and string2?sed use from android shellusing awk with variablesHow to use sed to replace a linux path in a specific text fileHow to convert multiline to singleline but preserve paragraphs






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















Say I have a variable var whose value is fOo bar1 baR2 bArab.



How do I go about saving into another variable, say, lc_var, a version of var where all but the first word are converted to lowercase?



I know it can look something like lc_var=$(echo $var | ...), where ... will replaced by an appropriate AWK or sed command.










share|improve this question







New contributor



lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2















    Say I have a variable var whose value is fOo bar1 baR2 bArab.



    How do I go about saving into another variable, say, lc_var, a version of var where all but the first word are converted to lowercase?



    I know it can look something like lc_var=$(echo $var | ...), where ... will replaced by an appropriate AWK or sed command.










    share|improve this question







    New contributor



    lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      2












      2








      2


      1






      Say I have a variable var whose value is fOo bar1 baR2 bArab.



      How do I go about saving into another variable, say, lc_var, a version of var where all but the first word are converted to lowercase?



      I know it can look something like lc_var=$(echo $var | ...), where ... will replaced by an appropriate AWK or sed command.










      share|improve this question







      New contributor



      lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      Say I have a variable var whose value is fOo bar1 baR2 bArab.



      How do I go about saving into another variable, say, lc_var, a version of var where all but the first word are converted to lowercase?



      I know it can look something like lc_var=$(echo $var | ...), where ... will replaced by an appropriate AWK or sed command.







      text-processing variable






      share|improve this question







      New contributor



      lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share|improve this question







      New contributor



      lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share|improve this question




      share|improve this question






      New contributor



      lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      asked Jun 9 at 19:49









      lowercaselowercase

      111




      111




      New contributor



      lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




      New contributor




      lowercase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          4 Answers
          4






          active

          oldest

          votes


















          4














          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.






          share|improve this answer

























          • Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.

            – seshoumara
            Jun 10 at 7:10






          • 1





            @seshoumara, awk doesn't process any input when there are only BEGIN statements (and optional function definitions).

            – Stéphane Chazelas
            Jun 10 at 7:11












          • Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.

            – seshoumara
            Jun 10 at 7:28











          • @seshoumara, GNU sed can do it with L as you've shown (that syntax coming from vi/ex initially IIRC), POSIX sed can do it with y but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)

            – Stéphane Chazelas
            Jun 10 at 7:31











          • I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.

            – seshoumara
            Jun 10 at 7:37



















          3














          With bash:



          var='fOo bar1 baR2 bArab'
          tail=$var#* ; lc_var="$var%% * $tail,,"
          echo "$lc_var"

          fOo bar1 bar2 barab


          as a function:



          set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,"; 


          or:



          set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t; 


          (this 2nd version should also work in ksh, and will handle an argument made up of a single word)



          set_lc var 'FOO BAR BAZ'
          echo "$var"

          FOO bar baz


          More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.






          share|improve this answer

























          • That doesn't work properly if the variable contains only one word (doesn't contain any space character)

            – Stéphane Chazelas
            Jun 10 at 5:03











          • The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless I in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l and $var,,.

            – pizdelect
            Jun 10 at 5:55



















          2














          The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.



          lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
          lc_var="$lc_var%."


          Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.






          share|improve this answer

























          • @StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.

            – seshoumara
            Jun 10 at 11:05



















          1














          lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')


          (with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)






          share|improve this answer

























            Your Answer








            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            lowercase is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523880%2fconvert-only-certain-words-to-lowercase%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            4














            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.






            share|improve this answer

























            • Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.

              – seshoumara
              Jun 10 at 7:10






            • 1





              @seshoumara, awk doesn't process any input when there are only BEGIN statements (and optional function definitions).

              – Stéphane Chazelas
              Jun 10 at 7:11












            • Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.

              – seshoumara
              Jun 10 at 7:28











            • @seshoumara, GNU sed can do it with L as you've shown (that syntax coming from vi/ex initially IIRC), POSIX sed can do it with y but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)

              – Stéphane Chazelas
              Jun 10 at 7:31











            • I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.

              – seshoumara
              Jun 10 at 7:37
















            4














            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.






            share|improve this answer

























            • Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.

              – seshoumara
              Jun 10 at 7:10






            • 1





              @seshoumara, awk doesn't process any input when there are only BEGIN statements (and optional function definitions).

              – Stéphane Chazelas
              Jun 10 at 7:11












            • Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.

              – seshoumara
              Jun 10 at 7:28











            • @seshoumara, GNU sed can do it with L as you've shown (that syntax coming from vi/ex initially IIRC), POSIX sed can do it with y but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)

              – Stéphane Chazelas
              Jun 10 at 7:31











            • I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.

              – seshoumara
              Jun 10 at 7:37














            4












            4








            4







            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.






            share|improve this answer















            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.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 10 at 7:16

























            answered Jun 9 at 20:23









            Stéphane ChazelasStéphane Chazelas

            322k57614985




            322k57614985












            • Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.

              – seshoumara
              Jun 10 at 7:10






            • 1





              @seshoumara, awk doesn't process any input when there are only BEGIN statements (and optional function definitions).

              – Stéphane Chazelas
              Jun 10 at 7:11












            • Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.

              – seshoumara
              Jun 10 at 7:28











            • @seshoumara, GNU sed can do it with L as you've shown (that syntax coming from vi/ex initially IIRC), POSIX sed can do it with y but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)

              – Stéphane Chazelas
              Jun 10 at 7:31











            • I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.

              – seshoumara
              Jun 10 at 7:37


















            • Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.

              – seshoumara
              Jun 10 at 7:10






            • 1





              @seshoumara, awk doesn't process any input when there are only BEGIN statements (and optional function definitions).

              – Stéphane Chazelas
              Jun 10 at 7:11












            • Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.

              – seshoumara
              Jun 10 at 7:28











            • @seshoumara, GNU sed can do it with L as you've shown (that syntax coming from vi/ex initially IIRC), POSIX sed can do it with y but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)

              – Stéphane Chazelas
              Jun 10 at 7:31











            • I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.

              – seshoumara
              Jun 10 at 7:37

















            Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.

            – seshoumara
            Jun 10 at 7:10





            Can you explain how is awk able to read that variable and not give an error like "awk: cannot open fOo bar1 baR2 bArab (No such file or directory)"? Thanks.

            – seshoumara
            Jun 10 at 7:10




            1




            1





            @seshoumara, awk doesn't process any input when there are only BEGIN statements (and optional function definitions).

            – Stéphane Chazelas
            Jun 10 at 7:11






            @seshoumara, awk doesn't process any input when there are only BEGIN statements (and optional function definitions).

            – Stéphane Chazelas
            Jun 10 at 7:11














            Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.

            – seshoumara
            Jun 10 at 7:28





            Cool! I'm just learning awk. Too bad sed can't do that, my favorite command. I changed my answer to not use echo either, based on that post you linked. I've been using echo in every other line in my bash scripts for years :|, time to use printf then.

            – seshoumara
            Jun 10 at 7:28













            @seshoumara, GNU sed can do it with L as you've shown (that syntax coming from vi/ex initially IIRC), POSIX sed can do it with y but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)

            – Stéphane Chazelas
            Jun 10 at 7:31





            @seshoumara, GNU sed can do it with L as you've shown (that syntax coming from vi/ex initially IIRC), POSIX sed can do it with y but you'd need to manually specify all the transliterations (AÁÂ -> aáâ...)

            – Stéphane Chazelas
            Jun 10 at 7:31













            I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.

            – seshoumara
            Jun 10 at 7:37






            I meant sed can't read a variable directly as awk does in your example by checking the arguments passed; sed always needs an input file, or stdin.

            – seshoumara
            Jun 10 at 7:37














            3














            With bash:



            var='fOo bar1 baR2 bArab'
            tail=$var#* ; lc_var="$var%% * $tail,,"
            echo "$lc_var"

            fOo bar1 bar2 barab


            as a function:



            set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,"; 


            or:



            set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t; 


            (this 2nd version should also work in ksh, and will handle an argument made up of a single word)



            set_lc var 'FOO BAR BAZ'
            echo "$var"

            FOO bar baz


            More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.






            share|improve this answer

























            • That doesn't work properly if the variable contains only one word (doesn't contain any space character)

              – Stéphane Chazelas
              Jun 10 at 5:03











            • The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless I in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l and $var,,.

              – pizdelect
              Jun 10 at 5:55
















            3














            With bash:



            var='fOo bar1 baR2 bArab'
            tail=$var#* ; lc_var="$var%% * $tail,,"
            echo "$lc_var"

            fOo bar1 bar2 barab


            as a function:



            set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,"; 


            or:



            set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t; 


            (this 2nd version should also work in ksh, and will handle an argument made up of a single word)



            set_lc var 'FOO BAR BAZ'
            echo "$var"

            FOO bar baz


            More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.






            share|improve this answer

























            • That doesn't work properly if the variable contains only one word (doesn't contain any space character)

              – Stéphane Chazelas
              Jun 10 at 5:03











            • The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless I in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l and $var,,.

              – pizdelect
              Jun 10 at 5:55














            3












            3








            3







            With bash:



            var='fOo bar1 baR2 bArab'
            tail=$var#* ; lc_var="$var%% * $tail,,"
            echo "$lc_var"

            fOo bar1 bar2 barab


            as a function:



            set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,"; 


            or:



            set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t; 


            (this 2nd version should also work in ksh, and will handle an argument made up of a single word)



            set_lc var 'FOO BAR BAZ'
            echo "$var"

            FOO bar baz


            More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.






            share|improve this answer















            With bash:



            var='fOo bar1 baR2 bArab'
            tail=$var#* ; lc_var="$var%% * $tail,,"
            echo "$lc_var"

            fOo bar1 bar2 barab


            as a function:



            set_lc() declare -n v=$1; declare t=$2#* ; v="$2%% * $t,,"; 


            or:



            set_lc() typeset -n _v=$1; typeset h=$2%% *; typeset -l t=$2#"$h"; _v=$h$t; 


            (this 2nd version should also work in ksh, and will handle an argument made up of a single word)



            set_lc var 'FOO BAR BAZ'
            echo "$var"

            FOO bar baz


            More info about bash's weird and limited parameter expansion modifiers and about declare / typeset in the bash manual.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 9 at 21:31

























            answered Jun 9 at 20:36









            pizdelectpizdelect

            1,316311




            1,316311












            • That doesn't work properly if the variable contains only one word (doesn't contain any space character)

              – Stéphane Chazelas
              Jun 10 at 5:03











            • The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless I in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l and $var,,.

              – pizdelect
              Jun 10 at 5:55


















            • That doesn't work properly if the variable contains only one word (doesn't contain any space character)

              – Stéphane Chazelas
              Jun 10 at 5:03











            • The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless I in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l and $var,,.

              – pizdelect
              Jun 10 at 5:55

















            That doesn't work properly if the variable contains only one word (doesn't contain any space character)

            – Stéphane Chazelas
            Jun 10 at 5:03





            That doesn't work properly if the variable contains only one word (doesn't contain any space character)

            – Stéphane Chazelas
            Jun 10 at 5:03













            The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless I in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l and $var,,.

            – pizdelect
            Jun 10 at 5:55






            The second version will. There are other limitations -- like not working with words separated by tabs, or correctly handling the dotless I in the turkish locale. I'll leave it as is, because the point of the examples was just to demonstrate the use of typeset -l and $var,,.

            – pizdelect
            Jun 10 at 5:55












            2














            The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.



            lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
            lc_var="$lc_var%."


            Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.






            share|improve this answer

























            • @StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.

              – seshoumara
              Jun 10 at 11:05
















            2














            The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.



            lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
            lc_var="$lc_var%."


            Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.






            share|improve this answer

























            • @StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.

              – seshoumara
              Jun 10 at 11:05














            2












            2








            2







            The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.



            lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
            lc_var="$lc_var%."


            Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.






            share|improve this answer















            The following solution uses GNU sed and was tested in bash only. It works even if the variable contains multi-line text.



            lc_var="$(printf "%sn" "$var"|sed ':l;$!N;bl;s:s.*:L&.:')"
            lc_var="$lc_var%."


            Many thanks to @Stéphane Chazelas for the discussion about edge cases, like echo, sed -z, command substitution and other things.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jun 10 at 11:01

























            answered Jun 9 at 19:59









            seshoumaraseshoumara

            45637




            45637












            • @StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.

              – seshoumara
              Jun 10 at 11:05


















            • @StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.

              – seshoumara
              Jun 10 at 11:05

















            @StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.

            – seshoumara
            Jun 10 at 11:05






            @StéphaneChazelas Updated my solution to treat some discussed edge cases. Since I need the elegant L in sed, I require the GNU version anyway, so posix was sacrificed.

            – seshoumara
            Jun 10 at 11:05












            1














            lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')


            (with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)






            share|improve this answer





























              1














              lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')


              (with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)






              share|improve this answer



























                1












                1








                1







                lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')


                (with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)






                share|improve this answer















                lc_var=$(echo $var | sed 's/^([^ ]*)(.*)/1L2/g')


                (with a little help from http://timmurphy.org/2013/02/24/converting-to-uppercase-lowercase-in-sed/)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 10 at 14:47









                Jeff Schaller

                47k1167152




                47k1167152










                answered Jun 10 at 12:24









                David YockeyDavid Yockey

                1287




                1287




















                    lowercase is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    lowercase is a new contributor. Be nice, and check out our Code of Conduct.












                    lowercase is a new contributor. Be nice, and check out our Code of Conduct.











                    lowercase is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523880%2fconvert-only-certain-words-to-lowercase%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                    Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?