Bash remove first and last characters from a string












85















I have a string like that:



|abcdefg|


And i want to get a new string called in someway (like string2) with the original string without the two | at the start and at the end of it



so that i will have this



abcdefg


is that possible in bash?










share|improve this question





























    85















    I have a string like that:



    |abcdefg|


    And i want to get a new string called in someway (like string2) with the original string without the two | at the start and at the end of it



    so that i will have this



    abcdefg


    is that possible in bash?










    share|improve this question



























      85












      85








      85


      18






      I have a string like that:



      |abcdefg|


      And i want to get a new string called in someway (like string2) with the original string without the two | at the start and at the end of it



      so that i will have this



      abcdefg


      is that possible in bash?










      share|improve this question
















      I have a string like that:



      |abcdefg|


      And i want to get a new string called in someway (like string2) with the original string without the two | at the start and at the end of it



      so that i will have this



      abcdefg


      is that possible in bash?







      bash command-line scripts






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 23 '11 at 14:48







      Matteo Pagliazzi

















      asked Dec 23 '11 at 14:29









      Matteo PagliazziMatteo Pagliazzi

      1,26751633




      1,26751633






















          8 Answers
          8






          active

          oldest

          votes


















          110














          You can do



          string="|abcdefg|"
          string2=${string#"|"}
          string2=${string2%"|"}
          echo $string2


          Or if your string length is constant, you can do



          string="|abcdefg|"
          string2=${string:1:7}
          echo $string2


          Also, this should work



          echo "|abcdefg|" | cut -d "|" -f 2


          Also this



          echo "|abcdefg|" | sed 's/^|(.*)|$/1/'





          share|improve this answer





















          • 2





            and also awk -F| '{ print $2 }' <<<"|string|"

            – enzotib
            Dec 23 '11 at 17:45











          • @enzotib You always have cool awk solutions. I need to learn awk.

            – Kris Harper
            Dec 23 '11 at 18:36








          • 2





            and also IFS='|' read string2 <<< $string :)

            – arrange
            Dec 23 '11 at 20:38






          • 9





            and also, in bash 4.2 and newer, "${string:1:-1}"

            – geirha
            Jul 5 '12 at 12:52













          • Read under the "parameter expansion" section in man bash.

            – Nemo
            Sep 5 '12 at 20:44



















          57














          Here's a solution that is independent of the length of the string (bash):



          string="|abcdefg|"
          echo "${string:1:${#string}-2}"





          share|improve this answer

































            23














            Going off a few posts listed here it seems the simplest way to do it is:



            string="|abcdefg|"
            echo ${string:1:-1}


            edit: works on ubuntu with bash 4.2; does not work on centOS with bash 4.1






            share|improve this answer

































              14














              Another way is to use head & tail commands:



              $ echo -n "|abcdefg|" | tail -c +2 | head -c -2
              abcdefg





              share|improve this answer





















              • 1





                I had a string of [something something] with a goal to cut brackets, so echo "[something something]" | tail -c +2 | head -c -2 worked out. Thanks for a tip!

                – Ain Tohvri
                Dec 13 '15 at 15:36



















              10














              And another one:



              string="|abcdefg|"
              echo "${string//|/}"





              share|improve this answer

































                8














                You can also use sed to remove the | not just referencing the symbol itself but using positional references as in:



                $ echo "|abcdefg|" | sed 's:^.(.*).$:1:'
                abcdefg


                Where ':' are the delimiters (you can replace them with / or any character not in the query, any sign following the s will do it) Here ^ (caret) means at the beginning of the input string and $ (dollar) means at the end. The . (point) that it's after the caret and the one that it's before the dollar sign represents a single character. So in other words we are deleting the first and last characters.
                Take in mind this will delete any characters even if | it's not present in the string.



                EX:



                $ echo "abcdefg" | sed 's:^.(.*).$:1:'
                bcdef





                share|improve this answer































                  2














                  shell function



                  A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those
                  we want to a new variable



                  Here's that whole idea formatted into a nice function



                  crop_string_ends() {
                  STR="$1"
                  NEWSTR=""
                  COUNT=0
                  while read -n 1 CHAR
                  do
                  COUNT=$(($COUNT+1))
                  if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ]
                  then
                  continue
                  fi
                  NEWSTR="$NEWSTR"$CHAR
                  done <<<"$STR"
                  echo $NEWSTR
                  }


                  And here is that same function in action:



                  $> crop_string_ends "|abcdefg|"                                                                                       
                  abcdefg
                  $> crop_string_ends "HelloWorld"
                  elloWorl


                  Python



                  >>> mystring="|abcdefg|"
                  >>> print(mystring[1:-1])
                  abcdefg


                  or on command line:



                  $ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|"                                             
                  abcdefg


                  AWK



                  $ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}'                                                      
                  abcdefg


                  Ruby



                  $ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"                                                               
                  abcdefg





                  share|improve this answer

































                    1














                    Small and universal solution:



                    expr "|abcdef|" : '.(.*).'


                    Special in this case and allowing that the '|' character may be there or not:



                    expr "|abcdef" : '|*([^|]*)|*'





                    share|improve this answer

























                      Your Answer








                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "89"
                      };
                      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: true,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: 10,
                      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
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f89995%2fbash-remove-first-and-last-characters-from-a-string%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      8 Answers
                      8






                      active

                      oldest

                      votes








                      8 Answers
                      8






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      110














                      You can do



                      string="|abcdefg|"
                      string2=${string#"|"}
                      string2=${string2%"|"}
                      echo $string2


                      Or if your string length is constant, you can do



                      string="|abcdefg|"
                      string2=${string:1:7}
                      echo $string2


                      Also, this should work



                      echo "|abcdefg|" | cut -d "|" -f 2


                      Also this



                      echo "|abcdefg|" | sed 's/^|(.*)|$/1/'





                      share|improve this answer





















                      • 2





                        and also awk -F| '{ print $2 }' <<<"|string|"

                        – enzotib
                        Dec 23 '11 at 17:45











                      • @enzotib You always have cool awk solutions. I need to learn awk.

                        – Kris Harper
                        Dec 23 '11 at 18:36








                      • 2





                        and also IFS='|' read string2 <<< $string :)

                        – arrange
                        Dec 23 '11 at 20:38






                      • 9





                        and also, in bash 4.2 and newer, "${string:1:-1}"

                        – geirha
                        Jul 5 '12 at 12:52













                      • Read under the "parameter expansion" section in man bash.

                        – Nemo
                        Sep 5 '12 at 20:44
















                      110














                      You can do



                      string="|abcdefg|"
                      string2=${string#"|"}
                      string2=${string2%"|"}
                      echo $string2


                      Or if your string length is constant, you can do



                      string="|abcdefg|"
                      string2=${string:1:7}
                      echo $string2


                      Also, this should work



                      echo "|abcdefg|" | cut -d "|" -f 2


                      Also this



                      echo "|abcdefg|" | sed 's/^|(.*)|$/1/'





                      share|improve this answer





















                      • 2





                        and also awk -F| '{ print $2 }' <<<"|string|"

                        – enzotib
                        Dec 23 '11 at 17:45











                      • @enzotib You always have cool awk solutions. I need to learn awk.

                        – Kris Harper
                        Dec 23 '11 at 18:36








                      • 2





                        and also IFS='|' read string2 <<< $string :)

                        – arrange
                        Dec 23 '11 at 20:38






                      • 9





                        and also, in bash 4.2 and newer, "${string:1:-1}"

                        – geirha
                        Jul 5 '12 at 12:52













                      • Read under the "parameter expansion" section in man bash.

                        – Nemo
                        Sep 5 '12 at 20:44














                      110












                      110








                      110







                      You can do



                      string="|abcdefg|"
                      string2=${string#"|"}
                      string2=${string2%"|"}
                      echo $string2


                      Or if your string length is constant, you can do



                      string="|abcdefg|"
                      string2=${string:1:7}
                      echo $string2


                      Also, this should work



                      echo "|abcdefg|" | cut -d "|" -f 2


                      Also this



                      echo "|abcdefg|" | sed 's/^|(.*)|$/1/'





                      share|improve this answer















                      You can do



                      string="|abcdefg|"
                      string2=${string#"|"}
                      string2=${string2%"|"}
                      echo $string2


                      Or if your string length is constant, you can do



                      string="|abcdefg|"
                      string2=${string:1:7}
                      echo $string2


                      Also, this should work



                      echo "|abcdefg|" | cut -d "|" -f 2


                      Also this



                      echo "|abcdefg|" | sed 's/^|(.*)|$/1/'






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 23 '11 at 18:36

























                      answered Dec 23 '11 at 14:49









                      Kris HarperKris Harper

                      9,629114771




                      9,629114771








                      • 2





                        and also awk -F| '{ print $2 }' <<<"|string|"

                        – enzotib
                        Dec 23 '11 at 17:45











                      • @enzotib You always have cool awk solutions. I need to learn awk.

                        – Kris Harper
                        Dec 23 '11 at 18:36








                      • 2





                        and also IFS='|' read string2 <<< $string :)

                        – arrange
                        Dec 23 '11 at 20:38






                      • 9





                        and also, in bash 4.2 and newer, "${string:1:-1}"

                        – geirha
                        Jul 5 '12 at 12:52













                      • Read under the "parameter expansion" section in man bash.

                        – Nemo
                        Sep 5 '12 at 20:44














                      • 2





                        and also awk -F| '{ print $2 }' <<<"|string|"

                        – enzotib
                        Dec 23 '11 at 17:45











                      • @enzotib You always have cool awk solutions. I need to learn awk.

                        – Kris Harper
                        Dec 23 '11 at 18:36








                      • 2





                        and also IFS='|' read string2 <<< $string :)

                        – arrange
                        Dec 23 '11 at 20:38






                      • 9





                        and also, in bash 4.2 and newer, "${string:1:-1}"

                        – geirha
                        Jul 5 '12 at 12:52













                      • Read under the "parameter expansion" section in man bash.

                        – Nemo
                        Sep 5 '12 at 20:44








                      2




                      2





                      and also awk -F| '{ print $2 }' <<<"|string|"

                      – enzotib
                      Dec 23 '11 at 17:45





                      and also awk -F| '{ print $2 }' <<<"|string|"

                      – enzotib
                      Dec 23 '11 at 17:45













                      @enzotib You always have cool awk solutions. I need to learn awk.

                      – Kris Harper
                      Dec 23 '11 at 18:36







                      @enzotib You always have cool awk solutions. I need to learn awk.

                      – Kris Harper
                      Dec 23 '11 at 18:36






                      2




                      2





                      and also IFS='|' read string2 <<< $string :)

                      – arrange
                      Dec 23 '11 at 20:38





                      and also IFS='|' read string2 <<< $string :)

                      – arrange
                      Dec 23 '11 at 20:38




                      9




                      9





                      and also, in bash 4.2 and newer, "${string:1:-1}"

                      – geirha
                      Jul 5 '12 at 12:52







                      and also, in bash 4.2 and newer, "${string:1:-1}"

                      – geirha
                      Jul 5 '12 at 12:52















                      Read under the "parameter expansion" section in man bash.

                      – Nemo
                      Sep 5 '12 at 20:44





                      Read under the "parameter expansion" section in man bash.

                      – Nemo
                      Sep 5 '12 at 20:44













                      57














                      Here's a solution that is independent of the length of the string (bash):



                      string="|abcdefg|"
                      echo "${string:1:${#string}-2}"





                      share|improve this answer






























                        57














                        Here's a solution that is independent of the length of the string (bash):



                        string="|abcdefg|"
                        echo "${string:1:${#string}-2}"





                        share|improve this answer




























                          57












                          57








                          57







                          Here's a solution that is independent of the length of the string (bash):



                          string="|abcdefg|"
                          echo "${string:1:${#string}-2}"





                          share|improve this answer















                          Here's a solution that is independent of the length of the string (bash):



                          string="|abcdefg|"
                          echo "${string:1:${#string}-2}"






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 5 '12 at 12:44









                          Eliah Kagan

                          81.9k21227364




                          81.9k21227364










                          answered Dec 24 '11 at 16:28









                          Samus_Samus_

                          76257




                          76257























                              23














                              Going off a few posts listed here it seems the simplest way to do it is:



                              string="|abcdefg|"
                              echo ${string:1:-1}


                              edit: works on ubuntu with bash 4.2; does not work on centOS with bash 4.1






                              share|improve this answer






























                                23














                                Going off a few posts listed here it seems the simplest way to do it is:



                                string="|abcdefg|"
                                echo ${string:1:-1}


                                edit: works on ubuntu with bash 4.2; does not work on centOS with bash 4.1






                                share|improve this answer




























                                  23












                                  23








                                  23







                                  Going off a few posts listed here it seems the simplest way to do it is:



                                  string="|abcdefg|"
                                  echo ${string:1:-1}


                                  edit: works on ubuntu with bash 4.2; does not work on centOS with bash 4.1






                                  share|improve this answer















                                  Going off a few posts listed here it seems the simplest way to do it is:



                                  string="|abcdefg|"
                                  echo ${string:1:-1}


                                  edit: works on ubuntu with bash 4.2; does not work on centOS with bash 4.1







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Oct 16 '12 at 16:32

























                                  answered Sep 5 '12 at 20:37









                                  jlunavtgradjlunavtgrad

                                  33924




                                  33924























                                      14














                                      Another way is to use head & tail commands:



                                      $ echo -n "|abcdefg|" | tail -c +2 | head -c -2
                                      abcdefg





                                      share|improve this answer





















                                      • 1





                                        I had a string of [something something] with a goal to cut brackets, so echo "[something something]" | tail -c +2 | head -c -2 worked out. Thanks for a tip!

                                        – Ain Tohvri
                                        Dec 13 '15 at 15:36
















                                      14














                                      Another way is to use head & tail commands:



                                      $ echo -n "|abcdefg|" | tail -c +2 | head -c -2
                                      abcdefg





                                      share|improve this answer





















                                      • 1





                                        I had a string of [something something] with a goal to cut brackets, so echo "[something something]" | tail -c +2 | head -c -2 worked out. Thanks for a tip!

                                        – Ain Tohvri
                                        Dec 13 '15 at 15:36














                                      14












                                      14








                                      14







                                      Another way is to use head & tail commands:



                                      $ echo -n "|abcdefg|" | tail -c +2 | head -c -2
                                      abcdefg





                                      share|improve this answer















                                      Another way is to use head & tail commands:



                                      $ echo -n "|abcdefg|" | tail -c +2 | head -c -2
                                      abcdefg






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jan 11 at 21:26









                                      agabrys

                                      1034




                                      1034










                                      answered Nov 26 '13 at 16:16









                                      ZaviorZavior

                                      24124




                                      24124








                                      • 1





                                        I had a string of [something something] with a goal to cut brackets, so echo "[something something]" | tail -c +2 | head -c -2 worked out. Thanks for a tip!

                                        – Ain Tohvri
                                        Dec 13 '15 at 15:36














                                      • 1





                                        I had a string of [something something] with a goal to cut brackets, so echo "[something something]" | tail -c +2 | head -c -2 worked out. Thanks for a tip!

                                        – Ain Tohvri
                                        Dec 13 '15 at 15:36








                                      1




                                      1





                                      I had a string of [something something] with a goal to cut brackets, so echo "[something something]" | tail -c +2 | head -c -2 worked out. Thanks for a tip!

                                      – Ain Tohvri
                                      Dec 13 '15 at 15:36





                                      I had a string of [something something] with a goal to cut brackets, so echo "[something something]" | tail -c +2 | head -c -2 worked out. Thanks for a tip!

                                      – Ain Tohvri
                                      Dec 13 '15 at 15:36











                                      10














                                      And another one:



                                      string="|abcdefg|"
                                      echo "${string//|/}"





                                      share|improve this answer






























                                        10














                                        And another one:



                                        string="|abcdefg|"
                                        echo "${string//|/}"





                                        share|improve this answer




























                                          10












                                          10








                                          10







                                          And another one:



                                          string="|abcdefg|"
                                          echo "${string//|/}"





                                          share|improve this answer















                                          And another one:



                                          string="|abcdefg|"
                                          echo "${string//|/}"






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Jul 18 '12 at 15:06

























                                          answered Jun 9 '12 at 1:03









                                          Steven PennySteven Penny

                                          1




                                          1























                                              8














                                              You can also use sed to remove the | not just referencing the symbol itself but using positional references as in:



                                              $ echo "|abcdefg|" | sed 's:^.(.*).$:1:'
                                              abcdefg


                                              Where ':' are the delimiters (you can replace them with / or any character not in the query, any sign following the s will do it) Here ^ (caret) means at the beginning of the input string and $ (dollar) means at the end. The . (point) that it's after the caret and the one that it's before the dollar sign represents a single character. So in other words we are deleting the first and last characters.
                                              Take in mind this will delete any characters even if | it's not present in the string.



                                              EX:



                                              $ echo "abcdefg" | sed 's:^.(.*).$:1:'
                                              bcdef





                                              share|improve this answer




























                                                8














                                                You can also use sed to remove the | not just referencing the symbol itself but using positional references as in:



                                                $ echo "|abcdefg|" | sed 's:^.(.*).$:1:'
                                                abcdefg


                                                Where ':' are the delimiters (you can replace them with / or any character not in the query, any sign following the s will do it) Here ^ (caret) means at the beginning of the input string and $ (dollar) means at the end. The . (point) that it's after the caret and the one that it's before the dollar sign represents a single character. So in other words we are deleting the first and last characters.
                                                Take in mind this will delete any characters even if | it's not present in the string.



                                                EX:



                                                $ echo "abcdefg" | sed 's:^.(.*).$:1:'
                                                bcdef





                                                share|improve this answer


























                                                  8












                                                  8








                                                  8







                                                  You can also use sed to remove the | not just referencing the symbol itself but using positional references as in:



                                                  $ echo "|abcdefg|" | sed 's:^.(.*).$:1:'
                                                  abcdefg


                                                  Where ':' are the delimiters (you can replace them with / or any character not in the query, any sign following the s will do it) Here ^ (caret) means at the beginning of the input string and $ (dollar) means at the end. The . (point) that it's after the caret and the one that it's before the dollar sign represents a single character. So in other words we are deleting the first and last characters.
                                                  Take in mind this will delete any characters even if | it's not present in the string.



                                                  EX:



                                                  $ echo "abcdefg" | sed 's:^.(.*).$:1:'
                                                  bcdef





                                                  share|improve this answer













                                                  You can also use sed to remove the | not just referencing the symbol itself but using positional references as in:



                                                  $ echo "|abcdefg|" | sed 's:^.(.*).$:1:'
                                                  abcdefg


                                                  Where ':' are the delimiters (you can replace them with / or any character not in the query, any sign following the s will do it) Here ^ (caret) means at the beginning of the input string and $ (dollar) means at the end. The . (point) that it's after the caret and the one that it's before the dollar sign represents a single character. So in other words we are deleting the first and last characters.
                                                  Take in mind this will delete any characters even if | it's not present in the string.



                                                  EX:



                                                  $ echo "abcdefg" | sed 's:^.(.*).$:1:'
                                                  bcdef






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Feb 8 '12 at 1:44









                                                  MetafanielMetafaniel

                                                  1893




                                                  1893























                                                      2














                                                      shell function



                                                      A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those
                                                      we want to a new variable



                                                      Here's that whole idea formatted into a nice function



                                                      crop_string_ends() {
                                                      STR="$1"
                                                      NEWSTR=""
                                                      COUNT=0
                                                      while read -n 1 CHAR
                                                      do
                                                      COUNT=$(($COUNT+1))
                                                      if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ]
                                                      then
                                                      continue
                                                      fi
                                                      NEWSTR="$NEWSTR"$CHAR
                                                      done <<<"$STR"
                                                      echo $NEWSTR
                                                      }


                                                      And here is that same function in action:



                                                      $> crop_string_ends "|abcdefg|"                                                                                       
                                                      abcdefg
                                                      $> crop_string_ends "HelloWorld"
                                                      elloWorl


                                                      Python



                                                      >>> mystring="|abcdefg|"
                                                      >>> print(mystring[1:-1])
                                                      abcdefg


                                                      or on command line:



                                                      $ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|"                                             
                                                      abcdefg


                                                      AWK



                                                      $ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}'                                                      
                                                      abcdefg


                                                      Ruby



                                                      $ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"                                                               
                                                      abcdefg





                                                      share|improve this answer






























                                                        2














                                                        shell function



                                                        A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those
                                                        we want to a new variable



                                                        Here's that whole idea formatted into a nice function



                                                        crop_string_ends() {
                                                        STR="$1"
                                                        NEWSTR=""
                                                        COUNT=0
                                                        while read -n 1 CHAR
                                                        do
                                                        COUNT=$(($COUNT+1))
                                                        if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ]
                                                        then
                                                        continue
                                                        fi
                                                        NEWSTR="$NEWSTR"$CHAR
                                                        done <<<"$STR"
                                                        echo $NEWSTR
                                                        }


                                                        And here is that same function in action:



                                                        $> crop_string_ends "|abcdefg|"                                                                                       
                                                        abcdefg
                                                        $> crop_string_ends "HelloWorld"
                                                        elloWorl


                                                        Python



                                                        >>> mystring="|abcdefg|"
                                                        >>> print(mystring[1:-1])
                                                        abcdefg


                                                        or on command line:



                                                        $ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|"                                             
                                                        abcdefg


                                                        AWK



                                                        $ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}'                                                      
                                                        abcdefg


                                                        Ruby



                                                        $ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"                                                               
                                                        abcdefg





                                                        share|improve this answer




























                                                          2












                                                          2








                                                          2







                                                          shell function



                                                          A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those
                                                          we want to a new variable



                                                          Here's that whole idea formatted into a nice function



                                                          crop_string_ends() {
                                                          STR="$1"
                                                          NEWSTR=""
                                                          COUNT=0
                                                          while read -n 1 CHAR
                                                          do
                                                          COUNT=$(($COUNT+1))
                                                          if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ]
                                                          then
                                                          continue
                                                          fi
                                                          NEWSTR="$NEWSTR"$CHAR
                                                          done <<<"$STR"
                                                          echo $NEWSTR
                                                          }


                                                          And here is that same function in action:



                                                          $> crop_string_ends "|abcdefg|"                                                                                       
                                                          abcdefg
                                                          $> crop_string_ends "HelloWorld"
                                                          elloWorl


                                                          Python



                                                          >>> mystring="|abcdefg|"
                                                          >>> print(mystring[1:-1])
                                                          abcdefg


                                                          or on command line:



                                                          $ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|"                                             
                                                          abcdefg


                                                          AWK



                                                          $ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}'                                                      
                                                          abcdefg


                                                          Ruby



                                                          $ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"                                                               
                                                          abcdefg





                                                          share|improve this answer















                                                          shell function



                                                          A bit more verbose approach, but works on any sort of first and last character, doesn't have to be the same. Basic idea is that we are taking a variable, reading it character by character, and appending only those
                                                          we want to a new variable



                                                          Here's that whole idea formatted into a nice function



                                                          crop_string_ends() {
                                                          STR="$1"
                                                          NEWSTR=""
                                                          COUNT=0
                                                          while read -n 1 CHAR
                                                          do
                                                          COUNT=$(($COUNT+1))
                                                          if [ $COUNT -eq 1 ] || [ $COUNT -eq ${#STR} ]
                                                          then
                                                          continue
                                                          fi
                                                          NEWSTR="$NEWSTR"$CHAR
                                                          done <<<"$STR"
                                                          echo $NEWSTR
                                                          }


                                                          And here is that same function in action:



                                                          $> crop_string_ends "|abcdefg|"                                                                                       
                                                          abcdefg
                                                          $> crop_string_ends "HelloWorld"
                                                          elloWorl


                                                          Python



                                                          >>> mystring="|abcdefg|"
                                                          >>> print(mystring[1:-1])
                                                          abcdefg


                                                          or on command line:



                                                          $ python -c 'import sys;print sys.stdin.read()[1:-2]' <<< "|abcdefg|"                                             
                                                          abcdefg


                                                          AWK



                                                          $ echo "|abcdefg|" | awk '{print substr($0,2,length($0)-2)}'                                                      
                                                          abcdefg


                                                          Ruby



                                                          $ ruby -ne 'print $_.split("|")[1]' <<< "|abcdefg|"                                                               
                                                          abcdefg






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Dec 24 '16 at 21:14

























                                                          answered Apr 6 '16 at 13:58









                                                          Sergiy KolodyazhnyySergiy Kolodyazhnyy

                                                          71.5k9147313




                                                          71.5k9147313























                                                              1














                                                              Small and universal solution:



                                                              expr "|abcdef|" : '.(.*).'


                                                              Special in this case and allowing that the '|' character may be there or not:



                                                              expr "|abcdef" : '|*([^|]*)|*'





                                                              share|improve this answer






























                                                                1














                                                                Small and universal solution:



                                                                expr "|abcdef|" : '.(.*).'


                                                                Special in this case and allowing that the '|' character may be there or not:



                                                                expr "|abcdef" : '|*([^|]*)|*'





                                                                share|improve this answer




























                                                                  1












                                                                  1








                                                                  1







                                                                  Small and universal solution:



                                                                  expr "|abcdef|" : '.(.*).'


                                                                  Special in this case and allowing that the '|' character may be there or not:



                                                                  expr "|abcdef" : '|*([^|]*)|*'





                                                                  share|improve this answer















                                                                  Small and universal solution:



                                                                  expr "|abcdef|" : '.(.*).'


                                                                  Special in this case and allowing that the '|' character may be there or not:



                                                                  expr "|abcdef" : '|*([^|]*)|*'






                                                                  share|improve this answer














                                                                  share|improve this answer



                                                                  share|improve this answer








                                                                  edited Jul 8 '17 at 23:37

























                                                                  answered Jul 8 '17 at 23:29









                                                                  Tosi DoTosi Do

                                                                  112




                                                                  112






























                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Ask Ubuntu!


                                                                      • 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%2faskubuntu.com%2fquestions%2f89995%2fbash-remove-first-and-last-characters-from-a-string%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

                                                                      flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

                                                                      Mangá

                                                                       ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕