Delete the last character of a string using string manipulation in shell script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












158















I would like to delete the last character of a string, I tried this little script :



#! /bin/sh 

t="lkj"
t=$t:-2
echo $t


but it prints "lkj", what I am doing wrong?










share|improve this question




























    158















    I would like to delete the last character of a string, I tried this little script :



    #! /bin/sh 

    t="lkj"
    t=$t:-2
    echo $t


    but it prints "lkj", what I am doing wrong?










    share|improve this question


























      158












      158








      158


      50






      I would like to delete the last character of a string, I tried this little script :



      #! /bin/sh 

      t="lkj"
      t=$t:-2
      echo $t


      but it prints "lkj", what I am doing wrong?










      share|improve this question
















      I would like to delete the last character of a string, I tried this little script :



      #! /bin/sh 

      t="lkj"
      t=$t:-2
      echo $t


      but it prints "lkj", what I am doing wrong?







      shell-script string






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 10 '16 at 11:48









      serenesat

      9341719




      9341719










      asked Jul 13 '14 at 13:29









      user3581976user3581976

      1,04531017




      1,04531017




















          14 Answers
          14






          active

          oldest

          votes


















          101














          In a POSIX shell, the syntax $t:-2 means something different - it expands to the value of t if t is set and non null, and otherwise to the value 2. To trim a single character by parameter expansion, the syntax you probably want is $t%?



          Note that in ksh93, bash or zsh, $t:(-2) or $t: -2 (note the space) are legal as a substring expansion but are probably not what you want, since they return the substring starting at a position 2 characters in from the end (i.e. it removes the first character i of the string ijk).



          See the Shell Parameter Expansion section of the Bash Reference Manual for more info:



          • Bash Reference Manual – Shell Parameter Expansion





          share|improve this answer




















          • 4





            Would you care to explain what is the magic behind '%?' ?

            – afraisse
            Apr 14 '16 at 15:36






          • 6





            @afraisse $parameter%word removes the shortest suffix pattern matching word - see the Parameter Expansion section of man bash

            – steeldriver
            Apr 14 '16 at 18:15






          • 1





            This works well for Bash 4.1.2: $t%? for folks stuck with CentOS/RHEL 6.x

            – Joey T
            Jan 12 '17 at 23:04


















          165














          With bash 4.2 and above, you can do:



          $var::-1


          Example:



          $ a=123
          $ echo "$a::-1"
          12


          Notice that for older bash ( for example, bash 3.2.5 on OS X), you should leave spaces between and after colons:



          $var: : -1





          share|improve this answer




















          • 12





            This works for bash version 4.2-alpha and above, too bad the version I have access to is earlier. :-/

            – h.j.k.
            Jul 14 '14 at 3:46






          • 2





            @iamaziz: From bash changelog, the negative length in $var:offset:lenght was added only in bash 4.2. Maybe OSX add its own patch for bash.

            – cuonglm
            May 8 '16 at 5:59











          • @cuonglm perhaps. I am on El Capitan and it only works if I either leave spaces, or explicitly put the start and end e.g. $v:2:-1.

            – iamaziz
            May 8 '16 at 6:38











          • @iamaziz: How about $v::(-1) or $v:: -1?

            – cuonglm
            May 8 '16 at 6:49












          • @cuonglm neither work :/

            – iamaziz
            May 8 '16 at 6:59


















          52














          for removing the last n characters from a line that makes no use of sed OR awk:



          > echo lkj | rev | cut -c (n+1)- | rev


          so for example you can delete the last character one character using this:



          > echo lkj | rev | cut -c 2- | rev

          > lk


          from rev manpage:




          DESCRIPTION

          The rev utility copies the specified files to the standard output,
          reversing the order of characters in every line. If no files are speci-
          fied, the standard input is read.




          UPDATE:



          if you don't know the length of the string, try:



          $ x="lkj"
          $ echo "$x%?"
          lk





          share|improve this answer




















          • 1





            Thank you for your response! The problem is that I don't know the size of the string, and are you sure that there isn't an easier way to do it?

            – user3581976
            Jul 13 '14 at 13:53











          • see Updates @user3581976

            – Networker
            Jul 13 '14 at 14:01


















          41














          Using sed it should be as fast as



          sed 's/.$//'


          Your single echo is then echo ljk | sed 's/.$//'.

          Using this, the 1-line string could be any size.






          share|improve this answer


















          • 10





            Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string.

            – Stéphane Chazelas
            Feb 1 '16 at 10:23


















          39














          A few options depending on the shell:



          • POSIX: t=$t%?

          • Bourne: t=`expr " $t" : ' (.*).'`

          • zsh/yash: t=$t[1,-2]

          • bash/zsh: t=$t:0:-1

          • ksh93/bash/zsh/mksh: t=$t:0:$#t-1

          • ksh93/bash/zsh/mksh: t=$t/%?

          • ksh93: t=$t/~(E).$/

          • es: @ t=$1 ~~ $t *?

          Note that while all are supposed to strip the last character, you'll find that some implementations (those that don't support multi-byte characters) strip the last byte instead (so would likely corrupt the last character if it was multi-byte).



          The expr variant assumes $t doesn't end in more than one newline character. It will also return a non-zero exit status if the resulting string ends up being 0 (or 000 or even -0 with some implementations). It could also give unexpected results if the string contains invalid characters.






          share|improve this answer

























          • Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too!

            – Russ
            Sep 16 '16 at 3:20











          • @Russ, t=$t%? is not Bourne but you're not likely to come across a Bourne shell nowadays. $t%? does work in all the other ones though.

            – Stéphane Chazelas
            Sep 16 '16 at 6:53











          • No fish shell option given! Probably more popular these days than ksh93...

            – rien333
            Nov 18 '17 at 1:21











          • @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well.

            – Stéphane Chazelas
            Nov 18 '17 at 8:10











          • Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1)

            – Avindra Goolcharan
            Apr 5 '18 at 20:41


















          17














          t=lkj
          echo $t:0:$#t-1


          You get a substring from 0 to the string length -1. Note however that this substraction is bash specific, and won't work on other shells.



          For instance, dash isn't able to parse even



          echo $t:0:$(expr $#t - 1)


          For example, on Ubuntu, /bin/sh is dash






          share|improve this answer
































            17














            The most portable, and shortest, answer is almost certainly:



            $t%?



            This works in bash, sh, ash, dash, busybox/ash, zsh, ksh, etc.



            It works by using old-school shell parameter expansion. Specifically, the % specifies to remove the smallest matching suffix of parameter t that matches the glob pattern ? (ie: any character).



            See "Remove Smallest Suffix Pattern" here for a (much) more detailed explanation and more background. Also see the docs for your shell (eg: man bash) under "parameter expansion".




            As a side note, if you wanted to remove the first character instead, you would use $t#?, since # matches from the front of the string (prefix) instead of the back (suffix).



            Also worth noting is that both % and # have %% and ## versions, which match the longest version of the given pattern instead of the shortest. Both $t%%? and $t##? would do the same as their single operator in this case, though (so don't add the useless extra character). This is because the given ? pattern only matches a single character. Mix in a * with some non-wildcards and things get more interesting with %% and ##.



            Understanding parameter expansions, or at least knowing about their existence and knowing how to look them up, is incredibly useful for writing and deciphering shell scripts of many flavors. Parameter expansions often look like arcane shell voodoo to many people because... well... they are arcane shell voodoo (although pretty well documented if you know to look for "parameter expansion"). Definitely good to have in the tool belt when you're stuck in a shell, though.






            share|improve this answer























            • This is the one that worked for me!

              – Michael J
              Feb 15 '18 at 23:37











            • nice thanks this one does also work on my kindle :)

              – Phil Roggenbuck
              Jun 21 '18 at 20:45


















            14














            You can also use head to print out all but the last character.



            $ s='i am a string'
            $ news=$(echo -n $s | head -c -1)
            $ echo $news
            i am a strin


            But unfortunately some versions of head do not include the leading - option. This is the case for the head that comes with OS X.






            share|improve this answer






























              5














              Thank you SteelDriver and Networker!



              if you don't know the length of the string, try:



              $ x="lkj"
              $ echo "$x%?"
              lk





              share|improve this answer
































                4














                It is easy enough to do using regular expression:



                n=2
                echo "lkj" | sed "s/(.*).$n/1/"





                share|improve this answer






























                  4














                  Some refinements. To remove more than one character, you can add multiple question marks. For example, to remove the last two characters from the variable: $SRC_IP_MSG, you can use:



                  SRC_IP_MSG=$SRC_IP_MSG%??





                  share|improve this answer
































                    3














                    Just to complete some possible usages of pure bash:



                    #!/bin/bash

                    # Testing substring removal
                    STR="Exemple string with trailing whitespace "
                    echo "'$STR'"
                    echo "Removed trailing whitespace: '$STR:0:$#STR-1'"
                    echo "Removed trailing whitespace: '$STR/% /'"


                    The first syntax takes a substring from a string, the syntax is
                    $STRING:OFFSET:LENGTH

                    For the second one, do notice the % sign, which means 'from end of line' and the syntax is
                    $STRING/PATTERN/SUBSTITUTION



                    And here are two shorter forms of the above mentioned



                    echo "Removed trailing whitespace: '$STR::-1'"
                    echo "Removed trailing whitespace: '$STR% '"


                    Here notice again the % sign, meaning 'Remove ( that is, replace with '' ) the shortest matched pattern (here represented by escaped space ' ' from the end of the PARAMETER - here named STR






                    share|improve this answer






























                      2














                      As we can also use php in command line, or shell scripts.
                      It is sometimes useful for surgical parsing.



                      php -r "echo substr('Hello', 0, -1);" 
                      // Output hell


                      With piping:



                      echo "hello" | php -r "echo substr(trim(fgets(STDIN)), 0, -1);"
                      // Output hell





                      share|improve this answer






























                        -1














                        In ksh:



                        echo $ORACLE_SID/%?/





                        share|improve this answer























                          protected by Kusalananda Sep 26 '17 at 21:11



                          Thank you for your interest in this question.
                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                          Would you like to answer one of these unanswered questions instead?














                          14 Answers
                          14






                          active

                          oldest

                          votes








                          14 Answers
                          14






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes









                          101














                          In a POSIX shell, the syntax $t:-2 means something different - it expands to the value of t if t is set and non null, and otherwise to the value 2. To trim a single character by parameter expansion, the syntax you probably want is $t%?



                          Note that in ksh93, bash or zsh, $t:(-2) or $t: -2 (note the space) are legal as a substring expansion but are probably not what you want, since they return the substring starting at a position 2 characters in from the end (i.e. it removes the first character i of the string ijk).



                          See the Shell Parameter Expansion section of the Bash Reference Manual for more info:



                          • Bash Reference Manual – Shell Parameter Expansion





                          share|improve this answer




















                          • 4





                            Would you care to explain what is the magic behind '%?' ?

                            – afraisse
                            Apr 14 '16 at 15:36






                          • 6





                            @afraisse $parameter%word removes the shortest suffix pattern matching word - see the Parameter Expansion section of man bash

                            – steeldriver
                            Apr 14 '16 at 18:15






                          • 1





                            This works well for Bash 4.1.2: $t%? for folks stuck with CentOS/RHEL 6.x

                            – Joey T
                            Jan 12 '17 at 23:04















                          101














                          In a POSIX shell, the syntax $t:-2 means something different - it expands to the value of t if t is set and non null, and otherwise to the value 2. To trim a single character by parameter expansion, the syntax you probably want is $t%?



                          Note that in ksh93, bash or zsh, $t:(-2) or $t: -2 (note the space) are legal as a substring expansion but are probably not what you want, since they return the substring starting at a position 2 characters in from the end (i.e. it removes the first character i of the string ijk).



                          See the Shell Parameter Expansion section of the Bash Reference Manual for more info:



                          • Bash Reference Manual – Shell Parameter Expansion





                          share|improve this answer




















                          • 4





                            Would you care to explain what is the magic behind '%?' ?

                            – afraisse
                            Apr 14 '16 at 15:36






                          • 6





                            @afraisse $parameter%word removes the shortest suffix pattern matching word - see the Parameter Expansion section of man bash

                            – steeldriver
                            Apr 14 '16 at 18:15






                          • 1





                            This works well for Bash 4.1.2: $t%? for folks stuck with CentOS/RHEL 6.x

                            – Joey T
                            Jan 12 '17 at 23:04













                          101












                          101








                          101







                          In a POSIX shell, the syntax $t:-2 means something different - it expands to the value of t if t is set and non null, and otherwise to the value 2. To trim a single character by parameter expansion, the syntax you probably want is $t%?



                          Note that in ksh93, bash or zsh, $t:(-2) or $t: -2 (note the space) are legal as a substring expansion but are probably not what you want, since they return the substring starting at a position 2 characters in from the end (i.e. it removes the first character i of the string ijk).



                          See the Shell Parameter Expansion section of the Bash Reference Manual for more info:



                          • Bash Reference Manual – Shell Parameter Expansion





                          share|improve this answer















                          In a POSIX shell, the syntax $t:-2 means something different - it expands to the value of t if t is set and non null, and otherwise to the value 2. To trim a single character by parameter expansion, the syntax you probably want is $t%?



                          Note that in ksh93, bash or zsh, $t:(-2) or $t: -2 (note the space) are legal as a substring expansion but are probably not what you want, since they return the substring starting at a position 2 characters in from the end (i.e. it removes the first character i of the string ijk).



                          See the Shell Parameter Expansion section of the Bash Reference Manual for more info:



                          • Bash Reference Manual – Shell Parameter Expansion






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Sep 30 '16 at 16:11









                          Kenny Evitt

                          15728




                          15728










                          answered Jul 13 '14 at 14:08









                          steeldriversteeldriver

                          37k45287




                          37k45287







                          • 4





                            Would you care to explain what is the magic behind '%?' ?

                            – afraisse
                            Apr 14 '16 at 15:36






                          • 6





                            @afraisse $parameter%word removes the shortest suffix pattern matching word - see the Parameter Expansion section of man bash

                            – steeldriver
                            Apr 14 '16 at 18:15






                          • 1





                            This works well for Bash 4.1.2: $t%? for folks stuck with CentOS/RHEL 6.x

                            – Joey T
                            Jan 12 '17 at 23:04












                          • 4





                            Would you care to explain what is the magic behind '%?' ?

                            – afraisse
                            Apr 14 '16 at 15:36






                          • 6





                            @afraisse $parameter%word removes the shortest suffix pattern matching word - see the Parameter Expansion section of man bash

                            – steeldriver
                            Apr 14 '16 at 18:15






                          • 1





                            This works well for Bash 4.1.2: $t%? for folks stuck with CentOS/RHEL 6.x

                            – Joey T
                            Jan 12 '17 at 23:04







                          4




                          4





                          Would you care to explain what is the magic behind '%?' ?

                          – afraisse
                          Apr 14 '16 at 15:36





                          Would you care to explain what is the magic behind '%?' ?

                          – afraisse
                          Apr 14 '16 at 15:36




                          6




                          6





                          @afraisse $parameter%word removes the shortest suffix pattern matching word - see the Parameter Expansion section of man bash

                          – steeldriver
                          Apr 14 '16 at 18:15





                          @afraisse $parameter%word removes the shortest suffix pattern matching word - see the Parameter Expansion section of man bash

                          – steeldriver
                          Apr 14 '16 at 18:15




                          1




                          1





                          This works well for Bash 4.1.2: $t%? for folks stuck with CentOS/RHEL 6.x

                          – Joey T
                          Jan 12 '17 at 23:04





                          This works well for Bash 4.1.2: $t%? for folks stuck with CentOS/RHEL 6.x

                          – Joey T
                          Jan 12 '17 at 23:04













                          165














                          With bash 4.2 and above, you can do:



                          $var::-1


                          Example:



                          $ a=123
                          $ echo "$a::-1"
                          12


                          Notice that for older bash ( for example, bash 3.2.5 on OS X), you should leave spaces between and after colons:



                          $var: : -1





                          share|improve this answer




















                          • 12





                            This works for bash version 4.2-alpha and above, too bad the version I have access to is earlier. :-/

                            – h.j.k.
                            Jul 14 '14 at 3:46






                          • 2





                            @iamaziz: From bash changelog, the negative length in $var:offset:lenght was added only in bash 4.2. Maybe OSX add its own patch for bash.

                            – cuonglm
                            May 8 '16 at 5:59











                          • @cuonglm perhaps. I am on El Capitan and it only works if I either leave spaces, or explicitly put the start and end e.g. $v:2:-1.

                            – iamaziz
                            May 8 '16 at 6:38











                          • @iamaziz: How about $v::(-1) or $v:: -1?

                            – cuonglm
                            May 8 '16 at 6:49












                          • @cuonglm neither work :/

                            – iamaziz
                            May 8 '16 at 6:59















                          165














                          With bash 4.2 and above, you can do:



                          $var::-1


                          Example:



                          $ a=123
                          $ echo "$a::-1"
                          12


                          Notice that for older bash ( for example, bash 3.2.5 on OS X), you should leave spaces between and after colons:



                          $var: : -1





                          share|improve this answer




















                          • 12





                            This works for bash version 4.2-alpha and above, too bad the version I have access to is earlier. :-/

                            – h.j.k.
                            Jul 14 '14 at 3:46






                          • 2





                            @iamaziz: From bash changelog, the negative length in $var:offset:lenght was added only in bash 4.2. Maybe OSX add its own patch for bash.

                            – cuonglm
                            May 8 '16 at 5:59











                          • @cuonglm perhaps. I am on El Capitan and it only works if I either leave spaces, or explicitly put the start and end e.g. $v:2:-1.

                            – iamaziz
                            May 8 '16 at 6:38











                          • @iamaziz: How about $v::(-1) or $v:: -1?

                            – cuonglm
                            May 8 '16 at 6:49












                          • @cuonglm neither work :/

                            – iamaziz
                            May 8 '16 at 6:59













                          165












                          165








                          165







                          With bash 4.2 and above, you can do:



                          $var::-1


                          Example:



                          $ a=123
                          $ echo "$a::-1"
                          12


                          Notice that for older bash ( for example, bash 3.2.5 on OS X), you should leave spaces between and after colons:



                          $var: : -1





                          share|improve this answer















                          With bash 4.2 and above, you can do:



                          $var::-1


                          Example:



                          $ a=123
                          $ echo "$a::-1"
                          12


                          Notice that for older bash ( for example, bash 3.2.5 on OS X), you should leave spaces between and after colons:



                          $var: : -1






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited May 8 '16 at 7:11









                          Community

                          1




                          1










                          answered Jul 13 '14 at 17:29









                          cuonglmcuonglm

                          105k25208307




                          105k25208307







                          • 12





                            This works for bash version 4.2-alpha and above, too bad the version I have access to is earlier. :-/

                            – h.j.k.
                            Jul 14 '14 at 3:46






                          • 2





                            @iamaziz: From bash changelog, the negative length in $var:offset:lenght was added only in bash 4.2. Maybe OSX add its own patch for bash.

                            – cuonglm
                            May 8 '16 at 5:59











                          • @cuonglm perhaps. I am on El Capitan and it only works if I either leave spaces, or explicitly put the start and end e.g. $v:2:-1.

                            – iamaziz
                            May 8 '16 at 6:38











                          • @iamaziz: How about $v::(-1) or $v:: -1?

                            – cuonglm
                            May 8 '16 at 6:49












                          • @cuonglm neither work :/

                            – iamaziz
                            May 8 '16 at 6:59












                          • 12





                            This works for bash version 4.2-alpha and above, too bad the version I have access to is earlier. :-/

                            – h.j.k.
                            Jul 14 '14 at 3:46






                          • 2





                            @iamaziz: From bash changelog, the negative length in $var:offset:lenght was added only in bash 4.2. Maybe OSX add its own patch for bash.

                            – cuonglm
                            May 8 '16 at 5:59











                          • @cuonglm perhaps. I am on El Capitan and it only works if I either leave spaces, or explicitly put the start and end e.g. $v:2:-1.

                            – iamaziz
                            May 8 '16 at 6:38











                          • @iamaziz: How about $v::(-1) or $v:: -1?

                            – cuonglm
                            May 8 '16 at 6:49












                          • @cuonglm neither work :/

                            – iamaziz
                            May 8 '16 at 6:59







                          12




                          12





                          This works for bash version 4.2-alpha and above, too bad the version I have access to is earlier. :-/

                          – h.j.k.
                          Jul 14 '14 at 3:46





                          This works for bash version 4.2-alpha and above, too bad the version I have access to is earlier. :-/

                          – h.j.k.
                          Jul 14 '14 at 3:46




                          2




                          2





                          @iamaziz: From bash changelog, the negative length in $var:offset:lenght was added only in bash 4.2. Maybe OSX add its own patch for bash.

                          – cuonglm
                          May 8 '16 at 5:59





                          @iamaziz: From bash changelog, the negative length in $var:offset:lenght was added only in bash 4.2. Maybe OSX add its own patch for bash.

                          – cuonglm
                          May 8 '16 at 5:59













                          @cuonglm perhaps. I am on El Capitan and it only works if I either leave spaces, or explicitly put the start and end e.g. $v:2:-1.

                          – iamaziz
                          May 8 '16 at 6:38





                          @cuonglm perhaps. I am on El Capitan and it only works if I either leave spaces, or explicitly put the start and end e.g. $v:2:-1.

                          – iamaziz
                          May 8 '16 at 6:38













                          @iamaziz: How about $v::(-1) or $v:: -1?

                          – cuonglm
                          May 8 '16 at 6:49






                          @iamaziz: How about $v::(-1) or $v:: -1?

                          – cuonglm
                          May 8 '16 at 6:49














                          @cuonglm neither work :/

                          – iamaziz
                          May 8 '16 at 6:59





                          @cuonglm neither work :/

                          – iamaziz
                          May 8 '16 at 6:59











                          52














                          for removing the last n characters from a line that makes no use of sed OR awk:



                          > echo lkj | rev | cut -c (n+1)- | rev


                          so for example you can delete the last character one character using this:



                          > echo lkj | rev | cut -c 2- | rev

                          > lk


                          from rev manpage:




                          DESCRIPTION

                          The rev utility copies the specified files to the standard output,
                          reversing the order of characters in every line. If no files are speci-
                          fied, the standard input is read.




                          UPDATE:



                          if you don't know the length of the string, try:



                          $ x="lkj"
                          $ echo "$x%?"
                          lk





                          share|improve this answer




















                          • 1





                            Thank you for your response! The problem is that I don't know the size of the string, and are you sure that there isn't an easier way to do it?

                            – user3581976
                            Jul 13 '14 at 13:53











                          • see Updates @user3581976

                            – Networker
                            Jul 13 '14 at 14:01















                          52














                          for removing the last n characters from a line that makes no use of sed OR awk:



                          > echo lkj | rev | cut -c (n+1)- | rev


                          so for example you can delete the last character one character using this:



                          > echo lkj | rev | cut -c 2- | rev

                          > lk


                          from rev manpage:




                          DESCRIPTION

                          The rev utility copies the specified files to the standard output,
                          reversing the order of characters in every line. If no files are speci-
                          fied, the standard input is read.




                          UPDATE:



                          if you don't know the length of the string, try:



                          $ x="lkj"
                          $ echo "$x%?"
                          lk





                          share|improve this answer




















                          • 1





                            Thank you for your response! The problem is that I don't know the size of the string, and are you sure that there isn't an easier way to do it?

                            – user3581976
                            Jul 13 '14 at 13:53











                          • see Updates @user3581976

                            – Networker
                            Jul 13 '14 at 14:01













                          52












                          52








                          52







                          for removing the last n characters from a line that makes no use of sed OR awk:



                          > echo lkj | rev | cut -c (n+1)- | rev


                          so for example you can delete the last character one character using this:



                          > echo lkj | rev | cut -c 2- | rev

                          > lk


                          from rev manpage:




                          DESCRIPTION

                          The rev utility copies the specified files to the standard output,
                          reversing the order of characters in every line. If no files are speci-
                          fied, the standard input is read.




                          UPDATE:



                          if you don't know the length of the string, try:



                          $ x="lkj"
                          $ echo "$x%?"
                          lk





                          share|improve this answer















                          for removing the last n characters from a line that makes no use of sed OR awk:



                          > echo lkj | rev | cut -c (n+1)- | rev


                          so for example you can delete the last character one character using this:



                          > echo lkj | rev | cut -c 2- | rev

                          > lk


                          from rev manpage:




                          DESCRIPTION

                          The rev utility copies the specified files to the standard output,
                          reversing the order of characters in every line. If no files are speci-
                          fied, the standard input is read.




                          UPDATE:



                          if you don't know the length of the string, try:



                          $ x="lkj"
                          $ echo "$x%?"
                          lk






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jul 13 '14 at 14:01

























                          answered Jul 13 '14 at 13:43









                          NetworkerNetworker

                          6,041104070




                          6,041104070







                          • 1





                            Thank you for your response! The problem is that I don't know the size of the string, and are you sure that there isn't an easier way to do it?

                            – user3581976
                            Jul 13 '14 at 13:53











                          • see Updates @user3581976

                            – Networker
                            Jul 13 '14 at 14:01












                          • 1





                            Thank you for your response! The problem is that I don't know the size of the string, and are you sure that there isn't an easier way to do it?

                            – user3581976
                            Jul 13 '14 at 13:53











                          • see Updates @user3581976

                            – Networker
                            Jul 13 '14 at 14:01







                          1




                          1





                          Thank you for your response! The problem is that I don't know the size of the string, and are you sure that there isn't an easier way to do it?

                          – user3581976
                          Jul 13 '14 at 13:53





                          Thank you for your response! The problem is that I don't know the size of the string, and are you sure that there isn't an easier way to do it?

                          – user3581976
                          Jul 13 '14 at 13:53













                          see Updates @user3581976

                          – Networker
                          Jul 13 '14 at 14:01





                          see Updates @user3581976

                          – Networker
                          Jul 13 '14 at 14:01











                          41














                          Using sed it should be as fast as



                          sed 's/.$//'


                          Your single echo is then echo ljk | sed 's/.$//'.

                          Using this, the 1-line string could be any size.






                          share|improve this answer


















                          • 10





                            Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string.

                            – Stéphane Chazelas
                            Feb 1 '16 at 10:23















                          41














                          Using sed it should be as fast as



                          sed 's/.$//'


                          Your single echo is then echo ljk | sed 's/.$//'.

                          Using this, the 1-line string could be any size.






                          share|improve this answer


















                          • 10





                            Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string.

                            – Stéphane Chazelas
                            Feb 1 '16 at 10:23













                          41












                          41








                          41







                          Using sed it should be as fast as



                          sed 's/.$//'


                          Your single echo is then echo ljk | sed 's/.$//'.

                          Using this, the 1-line string could be any size.






                          share|improve this answer













                          Using sed it should be as fast as



                          sed 's/.$//'


                          Your single echo is then echo ljk | sed 's/.$//'.

                          Using this, the 1-line string could be any size.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 16 '15 at 8:07









                          11111611711594591341111161171159459134

                          52144




                          52144







                          • 10





                            Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string.

                            – Stéphane Chazelas
                            Feb 1 '16 at 10:23












                          • 10





                            Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string.

                            – Stéphane Chazelas
                            Feb 1 '16 at 10:23







                          10




                          10





                          Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string.

                          – Stéphane Chazelas
                          Feb 1 '16 at 10:23





                          Note that in the general case, it doesn't delete the last character of the string, but the last character of every line of the string.

                          – Stéphane Chazelas
                          Feb 1 '16 at 10:23











                          39














                          A few options depending on the shell:



                          • POSIX: t=$t%?

                          • Bourne: t=`expr " $t" : ' (.*).'`

                          • zsh/yash: t=$t[1,-2]

                          • bash/zsh: t=$t:0:-1

                          • ksh93/bash/zsh/mksh: t=$t:0:$#t-1

                          • ksh93/bash/zsh/mksh: t=$t/%?

                          • ksh93: t=$t/~(E).$/

                          • es: @ t=$1 ~~ $t *?

                          Note that while all are supposed to strip the last character, you'll find that some implementations (those that don't support multi-byte characters) strip the last byte instead (so would likely corrupt the last character if it was multi-byte).



                          The expr variant assumes $t doesn't end in more than one newline character. It will also return a non-zero exit status if the resulting string ends up being 0 (or 000 or even -0 with some implementations). It could also give unexpected results if the string contains invalid characters.






                          share|improve this answer

























                          • Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too!

                            – Russ
                            Sep 16 '16 at 3:20











                          • @Russ, t=$t%? is not Bourne but you're not likely to come across a Bourne shell nowadays. $t%? does work in all the other ones though.

                            – Stéphane Chazelas
                            Sep 16 '16 at 6:53











                          • No fish shell option given! Probably more popular these days than ksh93...

                            – rien333
                            Nov 18 '17 at 1:21











                          • @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well.

                            – Stéphane Chazelas
                            Nov 18 '17 at 8:10











                          • Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1)

                            – Avindra Goolcharan
                            Apr 5 '18 at 20:41















                          39














                          A few options depending on the shell:



                          • POSIX: t=$t%?

                          • Bourne: t=`expr " $t" : ' (.*).'`

                          • zsh/yash: t=$t[1,-2]

                          • bash/zsh: t=$t:0:-1

                          • ksh93/bash/zsh/mksh: t=$t:0:$#t-1

                          • ksh93/bash/zsh/mksh: t=$t/%?

                          • ksh93: t=$t/~(E).$/

                          • es: @ t=$1 ~~ $t *?

                          Note that while all are supposed to strip the last character, you'll find that some implementations (those that don't support multi-byte characters) strip the last byte instead (so would likely corrupt the last character if it was multi-byte).



                          The expr variant assumes $t doesn't end in more than one newline character. It will also return a non-zero exit status if the resulting string ends up being 0 (or 000 or even -0 with some implementations). It could also give unexpected results if the string contains invalid characters.






                          share|improve this answer

























                          • Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too!

                            – Russ
                            Sep 16 '16 at 3:20











                          • @Russ, t=$t%? is not Bourne but you're not likely to come across a Bourne shell nowadays. $t%? does work in all the other ones though.

                            – Stéphane Chazelas
                            Sep 16 '16 at 6:53











                          • No fish shell option given! Probably more popular these days than ksh93...

                            – rien333
                            Nov 18 '17 at 1:21











                          • @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well.

                            – Stéphane Chazelas
                            Nov 18 '17 at 8:10











                          • Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1)

                            – Avindra Goolcharan
                            Apr 5 '18 at 20:41













                          39












                          39








                          39







                          A few options depending on the shell:



                          • POSIX: t=$t%?

                          • Bourne: t=`expr " $t" : ' (.*).'`

                          • zsh/yash: t=$t[1,-2]

                          • bash/zsh: t=$t:0:-1

                          • ksh93/bash/zsh/mksh: t=$t:0:$#t-1

                          • ksh93/bash/zsh/mksh: t=$t/%?

                          • ksh93: t=$t/~(E).$/

                          • es: @ t=$1 ~~ $t *?

                          Note that while all are supposed to strip the last character, you'll find that some implementations (those that don't support multi-byte characters) strip the last byte instead (so would likely corrupt the last character if it was multi-byte).



                          The expr variant assumes $t doesn't end in more than one newline character. It will also return a non-zero exit status if the resulting string ends up being 0 (or 000 or even -0 with some implementations). It could also give unexpected results if the string contains invalid characters.






                          share|improve this answer















                          A few options depending on the shell:



                          • POSIX: t=$t%?

                          • Bourne: t=`expr " $t" : ' (.*).'`

                          • zsh/yash: t=$t[1,-2]

                          • bash/zsh: t=$t:0:-1

                          • ksh93/bash/zsh/mksh: t=$t:0:$#t-1

                          • ksh93/bash/zsh/mksh: t=$t/%?

                          • ksh93: t=$t/~(E).$/

                          • es: @ t=$1 ~~ $t *?

                          Note that while all are supposed to strip the last character, you'll find that some implementations (those that don't support multi-byte characters) strip the last byte instead (so would likely corrupt the last character if it was multi-byte).



                          The expr variant assumes $t doesn't end in more than one newline character. It will also return a non-zero exit status if the resulting string ends up being 0 (or 000 or even -0 with some implementations). It could also give unexpected results if the string contains invalid characters.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Feb 18 at 16:40

























                          answered Feb 1 '16 at 10:19









                          Stéphane ChazelasStéphane Chazelas

                          310k57584945




                          310k57584945












                          • Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too!

                            – Russ
                            Sep 16 '16 at 3:20











                          • @Russ, t=$t%? is not Bourne but you're not likely to come across a Bourne shell nowadays. $t%? does work in all the other ones though.

                            – Stéphane Chazelas
                            Sep 16 '16 at 6:53











                          • No fish shell option given! Probably more popular these days than ksh93...

                            – rien333
                            Nov 18 '17 at 1:21











                          • @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well.

                            – Stéphane Chazelas
                            Nov 18 '17 at 8:10











                          • Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1)

                            – Avindra Goolcharan
                            Apr 5 '18 at 20:41

















                          • Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too!

                            – Russ
                            Sep 16 '16 at 3:20











                          • @Russ, t=$t%? is not Bourne but you're not likely to come across a Bourne shell nowadays. $t%? does work in all the other ones though.

                            – Stéphane Chazelas
                            Sep 16 '16 at 6:53











                          • No fish shell option given! Probably more popular these days than ksh93...

                            – rien333
                            Nov 18 '17 at 1:21











                          • @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well.

                            – Stéphane Chazelas
                            Nov 18 '17 at 8:10











                          • Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1)

                            – Avindra Goolcharan
                            Apr 5 '18 at 20:41
















                          Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too!

                          – Russ
                          Sep 16 '16 at 3:20





                          Nice and thorough! But... I assume all of those shells support POSIX, so everyone should just use that one to be the most portable. Smallest character count, too!

                          – Russ
                          Sep 16 '16 at 3:20













                          @Russ, t=$t%? is not Bourne but you're not likely to come across a Bourne shell nowadays. $t%? does work in all the other ones though.

                          – Stéphane Chazelas
                          Sep 16 '16 at 6:53





                          @Russ, t=$t%? is not Bourne but you're not likely to come across a Bourne shell nowadays. $t%? does work in all the other ones though.

                          – Stéphane Chazelas
                          Sep 16 '16 at 6:53













                          No fish shell option given! Probably more popular these days than ksh93...

                          – rien333
                          Nov 18 '17 at 1:21





                          No fish shell option given! Probably more popular these days than ksh93...

                          – rien333
                          Nov 18 '17 at 1:21













                          @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well.

                          – Stéphane Chazelas
                          Nov 18 '17 at 8:10





                          @rien333. I'd wait for the interface to stabilize a bit. fish is work in progress. 2.3.0 which introduced the string builtin was not released at the time of the Q&A. With the version I'm testing it on, you need string replace -r '(?s).z' '' -- $t (and I'd expect they'd want to change that, they should change the flags they pass to PCRE) or more convoluted ones. It also deals poorly with newline characters, and I know they're planning on changing that as well.

                          – Stéphane Chazelas
                          Nov 18 '17 at 8:10













                          Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1)

                          – Avindra Goolcharan
                          Apr 5 '18 at 20:41





                          Upvoted for the POSIX answer. confirmed working on Bash 3.2.57(1)

                          – Avindra Goolcharan
                          Apr 5 '18 at 20:41











                          17














                          t=lkj
                          echo $t:0:$#t-1


                          You get a substring from 0 to the string length -1. Note however that this substraction is bash specific, and won't work on other shells.



                          For instance, dash isn't able to parse even



                          echo $t:0:$(expr $#t - 1)


                          For example, on Ubuntu, /bin/sh is dash






                          share|improve this answer





























                            17














                            t=lkj
                            echo $t:0:$#t-1


                            You get a substring from 0 to the string length -1. Note however that this substraction is bash specific, and won't work on other shells.



                            For instance, dash isn't able to parse even



                            echo $t:0:$(expr $#t - 1)


                            For example, on Ubuntu, /bin/sh is dash






                            share|improve this answer



























                              17












                              17








                              17







                              t=lkj
                              echo $t:0:$#t-1


                              You get a substring from 0 to the string length -1. Note however that this substraction is bash specific, and won't work on other shells.



                              For instance, dash isn't able to parse even



                              echo $t:0:$(expr $#t - 1)


                              For example, on Ubuntu, /bin/sh is dash






                              share|improve this answer















                              t=lkj
                              echo $t:0:$#t-1


                              You get a substring from 0 to the string length -1. Note however that this substraction is bash specific, and won't work on other shells.



                              For instance, dash isn't able to parse even



                              echo $t:0:$(expr $#t - 1)


                              For example, on Ubuntu, /bin/sh is dash







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jul 13 '14 at 19:12









                              Volker Siegel

                              11.1k33261




                              11.1k33261










                              answered Jul 13 '14 at 18:22









                              ÁngelÁngel

                              1,378512




                              1,378512





















                                  17














                                  The most portable, and shortest, answer is almost certainly:



                                  $t%?



                                  This works in bash, sh, ash, dash, busybox/ash, zsh, ksh, etc.



                                  It works by using old-school shell parameter expansion. Specifically, the % specifies to remove the smallest matching suffix of parameter t that matches the glob pattern ? (ie: any character).



                                  See "Remove Smallest Suffix Pattern" here for a (much) more detailed explanation and more background. Also see the docs for your shell (eg: man bash) under "parameter expansion".




                                  As a side note, if you wanted to remove the first character instead, you would use $t#?, since # matches from the front of the string (prefix) instead of the back (suffix).



                                  Also worth noting is that both % and # have %% and ## versions, which match the longest version of the given pattern instead of the shortest. Both $t%%? and $t##? would do the same as their single operator in this case, though (so don't add the useless extra character). This is because the given ? pattern only matches a single character. Mix in a * with some non-wildcards and things get more interesting with %% and ##.



                                  Understanding parameter expansions, or at least knowing about their existence and knowing how to look them up, is incredibly useful for writing and deciphering shell scripts of many flavors. Parameter expansions often look like arcane shell voodoo to many people because... well... they are arcane shell voodoo (although pretty well documented if you know to look for "parameter expansion"). Definitely good to have in the tool belt when you're stuck in a shell, though.






                                  share|improve this answer























                                  • This is the one that worked for me!

                                    – Michael J
                                    Feb 15 '18 at 23:37











                                  • nice thanks this one does also work on my kindle :)

                                    – Phil Roggenbuck
                                    Jun 21 '18 at 20:45















                                  17














                                  The most portable, and shortest, answer is almost certainly:



                                  $t%?



                                  This works in bash, sh, ash, dash, busybox/ash, zsh, ksh, etc.



                                  It works by using old-school shell parameter expansion. Specifically, the % specifies to remove the smallest matching suffix of parameter t that matches the glob pattern ? (ie: any character).



                                  See "Remove Smallest Suffix Pattern" here for a (much) more detailed explanation and more background. Also see the docs for your shell (eg: man bash) under "parameter expansion".




                                  As a side note, if you wanted to remove the first character instead, you would use $t#?, since # matches from the front of the string (prefix) instead of the back (suffix).



                                  Also worth noting is that both % and # have %% and ## versions, which match the longest version of the given pattern instead of the shortest. Both $t%%? and $t##? would do the same as their single operator in this case, though (so don't add the useless extra character). This is because the given ? pattern only matches a single character. Mix in a * with some non-wildcards and things get more interesting with %% and ##.



                                  Understanding parameter expansions, or at least knowing about their existence and knowing how to look them up, is incredibly useful for writing and deciphering shell scripts of many flavors. Parameter expansions often look like arcane shell voodoo to many people because... well... they are arcane shell voodoo (although pretty well documented if you know to look for "parameter expansion"). Definitely good to have in the tool belt when you're stuck in a shell, though.






                                  share|improve this answer























                                  • This is the one that worked for me!

                                    – Michael J
                                    Feb 15 '18 at 23:37











                                  • nice thanks this one does also work on my kindle :)

                                    – Phil Roggenbuck
                                    Jun 21 '18 at 20:45













                                  17












                                  17








                                  17







                                  The most portable, and shortest, answer is almost certainly:



                                  $t%?



                                  This works in bash, sh, ash, dash, busybox/ash, zsh, ksh, etc.



                                  It works by using old-school shell parameter expansion. Specifically, the % specifies to remove the smallest matching suffix of parameter t that matches the glob pattern ? (ie: any character).



                                  See "Remove Smallest Suffix Pattern" here for a (much) more detailed explanation and more background. Also see the docs for your shell (eg: man bash) under "parameter expansion".




                                  As a side note, if you wanted to remove the first character instead, you would use $t#?, since # matches from the front of the string (prefix) instead of the back (suffix).



                                  Also worth noting is that both % and # have %% and ## versions, which match the longest version of the given pattern instead of the shortest. Both $t%%? and $t##? would do the same as their single operator in this case, though (so don't add the useless extra character). This is because the given ? pattern only matches a single character. Mix in a * with some non-wildcards and things get more interesting with %% and ##.



                                  Understanding parameter expansions, or at least knowing about their existence and knowing how to look them up, is incredibly useful for writing and deciphering shell scripts of many flavors. Parameter expansions often look like arcane shell voodoo to many people because... well... they are arcane shell voodoo (although pretty well documented if you know to look for "parameter expansion"). Definitely good to have in the tool belt when you're stuck in a shell, though.






                                  share|improve this answer













                                  The most portable, and shortest, answer is almost certainly:



                                  $t%?



                                  This works in bash, sh, ash, dash, busybox/ash, zsh, ksh, etc.



                                  It works by using old-school shell parameter expansion. Specifically, the % specifies to remove the smallest matching suffix of parameter t that matches the glob pattern ? (ie: any character).



                                  See "Remove Smallest Suffix Pattern" here for a (much) more detailed explanation and more background. Also see the docs for your shell (eg: man bash) under "parameter expansion".




                                  As a side note, if you wanted to remove the first character instead, you would use $t#?, since # matches from the front of the string (prefix) instead of the back (suffix).



                                  Also worth noting is that both % and # have %% and ## versions, which match the longest version of the given pattern instead of the shortest. Both $t%%? and $t##? would do the same as their single operator in this case, though (so don't add the useless extra character). This is because the given ? pattern only matches a single character. Mix in a * with some non-wildcards and things get more interesting with %% and ##.



                                  Understanding parameter expansions, or at least knowing about their existence and knowing how to look them up, is incredibly useful for writing and deciphering shell scripts of many flavors. Parameter expansions often look like arcane shell voodoo to many people because... well... they are arcane shell voodoo (although pretty well documented if you know to look for "parameter expansion"). Definitely good to have in the tool belt when you're stuck in a shell, though.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Sep 16 '16 at 4:49









                                  RussRuss

                                  42859




                                  42859












                                  • This is the one that worked for me!

                                    – Michael J
                                    Feb 15 '18 at 23:37











                                  • nice thanks this one does also work on my kindle :)

                                    – Phil Roggenbuck
                                    Jun 21 '18 at 20:45

















                                  • This is the one that worked for me!

                                    – Michael J
                                    Feb 15 '18 at 23:37











                                  • nice thanks this one does also work on my kindle :)

                                    – Phil Roggenbuck
                                    Jun 21 '18 at 20:45
















                                  This is the one that worked for me!

                                  – Michael J
                                  Feb 15 '18 at 23:37





                                  This is the one that worked for me!

                                  – Michael J
                                  Feb 15 '18 at 23:37













                                  nice thanks this one does also work on my kindle :)

                                  – Phil Roggenbuck
                                  Jun 21 '18 at 20:45





                                  nice thanks this one does also work on my kindle :)

                                  – Phil Roggenbuck
                                  Jun 21 '18 at 20:45











                                  14














                                  You can also use head to print out all but the last character.



                                  $ s='i am a string'
                                  $ news=$(echo -n $s | head -c -1)
                                  $ echo $news
                                  i am a strin


                                  But unfortunately some versions of head do not include the leading - option. This is the case for the head that comes with OS X.






                                  share|improve this answer



























                                    14














                                    You can also use head to print out all but the last character.



                                    $ s='i am a string'
                                    $ news=$(echo -n $s | head -c -1)
                                    $ echo $news
                                    i am a strin


                                    But unfortunately some versions of head do not include the leading - option. This is the case for the head that comes with OS X.






                                    share|improve this answer

























                                      14












                                      14








                                      14







                                      You can also use head to print out all but the last character.



                                      $ s='i am a string'
                                      $ news=$(echo -n $s | head -c -1)
                                      $ echo $news
                                      i am a strin


                                      But unfortunately some versions of head do not include the leading - option. This is the case for the head that comes with OS X.






                                      share|improve this answer













                                      You can also use head to print out all but the last character.



                                      $ s='i am a string'
                                      $ news=$(echo -n $s | head -c -1)
                                      $ echo $news
                                      i am a strin


                                      But unfortunately some versions of head do not include the leading - option. This is the case for the head that comes with OS X.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 10 '15 at 17:31









                                      greenbeansugargreenbeansugar

                                      14113




                                      14113





















                                          5














                                          Thank you SteelDriver and Networker!



                                          if you don't know the length of the string, try:



                                          $ x="lkj"
                                          $ echo "$x%?"
                                          lk





                                          share|improve this answer





























                                            5














                                            Thank you SteelDriver and Networker!



                                            if you don't know the length of the string, try:



                                            $ x="lkj"
                                            $ echo "$x%?"
                                            lk





                                            share|improve this answer



























                                              5












                                              5








                                              5







                                              Thank you SteelDriver and Networker!



                                              if you don't know the length of the string, try:



                                              $ x="lkj"
                                              $ echo "$x%?"
                                              lk





                                              share|improve this answer















                                              Thank you SteelDriver and Networker!



                                              if you don't know the length of the string, try:



                                              $ x="lkj"
                                              $ echo "$x%?"
                                              lk






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 26 '14 at 22:35









                                              Anthon

                                              61.2k17104168




                                              61.2k17104168










                                              answered Nov 26 '14 at 22:23









                                              drilldrill

                                              5111




                                              5111





















                                                  4














                                                  It is easy enough to do using regular expression:



                                                  n=2
                                                  echo "lkj" | sed "s/(.*).$n/1/"





                                                  share|improve this answer



























                                                    4














                                                    It is easy enough to do using regular expression:



                                                    n=2
                                                    echo "lkj" | sed "s/(.*).$n/1/"





                                                    share|improve this answer

























                                                      4












                                                      4








                                                      4







                                                      It is easy enough to do using regular expression:



                                                      n=2
                                                      echo "lkj" | sed "s/(.*).$n/1/"





                                                      share|improve this answer













                                                      It is easy enough to do using regular expression:



                                                      n=2
                                                      echo "lkj" | sed "s/(.*).$n/1/"






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Jul 13 '14 at 13:49









                                                      unxnutunxnut

                                                      3,75721020




                                                      3,75721020





















                                                          4














                                                          Some refinements. To remove more than one character, you can add multiple question marks. For example, to remove the last two characters from the variable: $SRC_IP_MSG, you can use:



                                                          SRC_IP_MSG=$SRC_IP_MSG%??





                                                          share|improve this answer





























                                                            4














                                                            Some refinements. To remove more than one character, you can add multiple question marks. For example, to remove the last two characters from the variable: $SRC_IP_MSG, you can use:



                                                            SRC_IP_MSG=$SRC_IP_MSG%??





                                                            share|improve this answer



























                                                              4












                                                              4








                                                              4







                                                              Some refinements. To remove more than one character, you can add multiple question marks. For example, to remove the last two characters from the variable: $SRC_IP_MSG, you can use:



                                                              SRC_IP_MSG=$SRC_IP_MSG%??





                                                              share|improve this answer















                                                              Some refinements. To remove more than one character, you can add multiple question marks. For example, to remove the last two characters from the variable: $SRC_IP_MSG, you can use:



                                                              SRC_IP_MSG=$SRC_IP_MSG%??






                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Feb 1 '16 at 11:05









                                                              Kevdog777

                                                              2,111123360




                                                              2,111123360










                                                              answered Feb 1 '16 at 9:54









                                                              yuliskovyuliskov

                                                              1413




                                                              1413





















                                                                  3














                                                                  Just to complete some possible usages of pure bash:



                                                                  #!/bin/bash

                                                                  # Testing substring removal
                                                                  STR="Exemple string with trailing whitespace "
                                                                  echo "'$STR'"
                                                                  echo "Removed trailing whitespace: '$STR:0:$#STR-1'"
                                                                  echo "Removed trailing whitespace: '$STR/% /'"


                                                                  The first syntax takes a substring from a string, the syntax is
                                                                  $STRING:OFFSET:LENGTH

                                                                  For the second one, do notice the % sign, which means 'from end of line' and the syntax is
                                                                  $STRING/PATTERN/SUBSTITUTION



                                                                  And here are two shorter forms of the above mentioned



                                                                  echo "Removed trailing whitespace: '$STR::-1'"
                                                                  echo "Removed trailing whitespace: '$STR% '"


                                                                  Here notice again the % sign, meaning 'Remove ( that is, replace with '' ) the shortest matched pattern (here represented by escaped space ' ' from the end of the PARAMETER - here named STR






                                                                  share|improve this answer



























                                                                    3














                                                                    Just to complete some possible usages of pure bash:



                                                                    #!/bin/bash

                                                                    # Testing substring removal
                                                                    STR="Exemple string with trailing whitespace "
                                                                    echo "'$STR'"
                                                                    echo "Removed trailing whitespace: '$STR:0:$#STR-1'"
                                                                    echo "Removed trailing whitespace: '$STR/% /'"


                                                                    The first syntax takes a substring from a string, the syntax is
                                                                    $STRING:OFFSET:LENGTH

                                                                    For the second one, do notice the % sign, which means 'from end of line' and the syntax is
                                                                    $STRING/PATTERN/SUBSTITUTION



                                                                    And here are two shorter forms of the above mentioned



                                                                    echo "Removed trailing whitespace: '$STR::-1'"
                                                                    echo "Removed trailing whitespace: '$STR% '"


                                                                    Here notice again the % sign, meaning 'Remove ( that is, replace with '' ) the shortest matched pattern (here represented by escaped space ' ' from the end of the PARAMETER - here named STR






                                                                    share|improve this answer

























                                                                      3












                                                                      3








                                                                      3







                                                                      Just to complete some possible usages of pure bash:



                                                                      #!/bin/bash

                                                                      # Testing substring removal
                                                                      STR="Exemple string with trailing whitespace "
                                                                      echo "'$STR'"
                                                                      echo "Removed trailing whitespace: '$STR:0:$#STR-1'"
                                                                      echo "Removed trailing whitespace: '$STR/% /'"


                                                                      The first syntax takes a substring from a string, the syntax is
                                                                      $STRING:OFFSET:LENGTH

                                                                      For the second one, do notice the % sign, which means 'from end of line' and the syntax is
                                                                      $STRING/PATTERN/SUBSTITUTION



                                                                      And here are two shorter forms of the above mentioned



                                                                      echo "Removed trailing whitespace: '$STR::-1'"
                                                                      echo "Removed trailing whitespace: '$STR% '"


                                                                      Here notice again the % sign, meaning 'Remove ( that is, replace with '' ) the shortest matched pattern (here represented by escaped space ' ' from the end of the PARAMETER - here named STR






                                                                      share|improve this answer













                                                                      Just to complete some possible usages of pure bash:



                                                                      #!/bin/bash

                                                                      # Testing substring removal
                                                                      STR="Exemple string with trailing whitespace "
                                                                      echo "'$STR'"
                                                                      echo "Removed trailing whitespace: '$STR:0:$#STR-1'"
                                                                      echo "Removed trailing whitespace: '$STR/% /'"


                                                                      The first syntax takes a substring from a string, the syntax is
                                                                      $STRING:OFFSET:LENGTH

                                                                      For the second one, do notice the % sign, which means 'from end of line' and the syntax is
                                                                      $STRING/PATTERN/SUBSTITUTION



                                                                      And here are two shorter forms of the above mentioned



                                                                      echo "Removed trailing whitespace: '$STR::-1'"
                                                                      echo "Removed trailing whitespace: '$STR% '"


                                                                      Here notice again the % sign, meaning 'Remove ( that is, replace with '' ) the shortest matched pattern (here represented by escaped space ' ' from the end of the PARAMETER - here named STR







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Sep 26 '17 at 17:17









                                                                      CermakMCermakM

                                                                      20123




                                                                      20123





















                                                                          2














                                                                          As we can also use php in command line, or shell scripts.
                                                                          It is sometimes useful for surgical parsing.



                                                                          php -r "echo substr('Hello', 0, -1);" 
                                                                          // Output hell


                                                                          With piping:



                                                                          echo "hello" | php -r "echo substr(trim(fgets(STDIN)), 0, -1);"
                                                                          // Output hell





                                                                          share|improve this answer



























                                                                            2














                                                                            As we can also use php in command line, or shell scripts.
                                                                            It is sometimes useful for surgical parsing.



                                                                            php -r "echo substr('Hello', 0, -1);" 
                                                                            // Output hell


                                                                            With piping:



                                                                            echo "hello" | php -r "echo substr(trim(fgets(STDIN)), 0, -1);"
                                                                            // Output hell





                                                                            share|improve this answer

























                                                                              2












                                                                              2








                                                                              2







                                                                              As we can also use php in command line, or shell scripts.
                                                                              It is sometimes useful for surgical parsing.



                                                                              php -r "echo substr('Hello', 0, -1);" 
                                                                              // Output hell


                                                                              With piping:



                                                                              echo "hello" | php -r "echo substr(trim(fgets(STDIN)), 0, -1);"
                                                                              // Output hell





                                                                              share|improve this answer













                                                                              As we can also use php in command line, or shell scripts.
                                                                              It is sometimes useful for surgical parsing.



                                                                              php -r "echo substr('Hello', 0, -1);" 
                                                                              // Output hell


                                                                              With piping:



                                                                              echo "hello" | php -r "echo substr(trim(fgets(STDIN)), 0, -1);"
                                                                              // Output hell






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Feb 5 '18 at 23:48









                                                                              CryptopatCryptopat

                                                                              1756




                                                                              1756





















                                                                                  -1














                                                                                  In ksh:



                                                                                  echo $ORACLE_SID/%?/





                                                                                  share|improve this answer





























                                                                                    -1














                                                                                    In ksh:



                                                                                    echo $ORACLE_SID/%?/





                                                                                    share|improve this answer



























                                                                                      -1












                                                                                      -1








                                                                                      -1







                                                                                      In ksh:



                                                                                      echo $ORACLE_SID/%?/





                                                                                      share|improve this answer















                                                                                      In ksh:



                                                                                      echo $ORACLE_SID/%?/






                                                                                      share|improve this answer














                                                                                      share|improve this answer



                                                                                      share|improve this answer








                                                                                      edited Apr 12 '16 at 4:40









                                                                                      techraf

                                                                                      4,235102242




                                                                                      4,235102242










                                                                                      answered Apr 12 '16 at 4:15









                                                                                      AlexAlex

                                                                                      1




                                                                                      1















                                                                                          protected by Kusalananda Sep 26 '17 at 21:11



                                                                                          Thank you for your interest in this question.
                                                                                          Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                          Would you like to answer one of these unanswered questions instead?


                                                                                          Popular posts from this blog

                                                                                          How to check contact read email or not when send email to Individual?

                                                                                          Bahrain

                                                                                          Postfix configuration issue with fips on centos 7; mailgun relay