Can't chop last X letters from a string [closed]checking an argument to a bash script is a string of all digitsMatching regular expression with delimited string in unixHow to append extension to files with certain filename formatHow can I make sure that I'm running an updated version of bash?Can't use global shorcuts for Python applicationsWhy is my regex not working using sed in bash script on Mac OSX?Count the number of occurrences of a substring in a stringCompare datetime stringBash: Set variable equal to a string substitutionhow to convert a string to a float and then use it in a conditional operation?

Is it possible to do 50 km distance without any previous training?

Why is the design of haulage companies so “special”?

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?

"which" command doesn't work / path of Safari?

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

Why CLRS example on residual networks does not follows its formula?

Are tax years 2016 & 2017 back taxes deductible for tax year 2018?

How much RAM could one put in a typical 80386 setup?

How can I fix this gap between bookcases I made?

N.B. ligature in Latex

What defenses are there against being summoned by the Gate spell?

Can a German sentence have two subjects?

Why don't electron-positron collisions release infinite energy?

whey we use polarized capacitor?

What typically incentivizes a professor to change jobs to a lower ranking university?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Why is an old chain unsafe?

Why Is Death Allowed In the Matrix?

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

Can Medicine checks be used, with decent rolls, to completely mitigate the risk of death from ongoing damage?

Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)

Prevent a directory in /tmp from being deleted

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

Modification to Chariots for Heavy Cavalry Analogue for 4-armed race



Can't chop last X letters from a string [closed]


checking an argument to a bash script is a string of all digitsMatching regular expression with delimited string in unixHow to append extension to files with certain filename formatHow can I make sure that I'm running an updated version of bash?Can't use global shorcuts for Python applicationsWhy is my regex not working using sed in bash script on Mac OSX?Count the number of occurrences of a substring in a stringCompare datetime stringBash: Set variable equal to a string substitutionhow to convert a string to a float and then use it in a conditional operation?






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








0















I'm on a mac, bash version 5.0.2.



Got this:



 v="ksdjfksjkjsdf"
v2=$v::-6


Get this:



-bash: -6: substring expression < 0









share|improve this question













closed as off-topic by Jesse_b, ilkkachu, terdon Mar 27 at 19:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
If this question can be reworded to fit the rules in the help center, please edit the question.
















  • Which version of bash? I just tried this with 4.4.12 and it worked as expected.

    – Panki
    Mar 27 at 14:30











  • at the top of the post: 5.0.2

    – StevieD
    Mar 27 at 14:31











  • Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried v2=$v::-14 it would

    – Jesse_b
    Mar 27 at 14:42












  • Positive. Yes. That's all I have in my test script besides shebang.

    – StevieD
    Mar 27 at 14:49

















0















I'm on a mac, bash version 5.0.2.



Got this:



 v="ksdjfksjkjsdf"
v2=$v::-6


Get this:



-bash: -6: substring expression < 0









share|improve this question













closed as off-topic by Jesse_b, ilkkachu, terdon Mar 27 at 19:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
If this question can be reworded to fit the rules in the help center, please edit the question.
















  • Which version of bash? I just tried this with 4.4.12 and it worked as expected.

    – Panki
    Mar 27 at 14:30











  • at the top of the post: 5.0.2

    – StevieD
    Mar 27 at 14:31











  • Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried v2=$v::-14 it would

    – Jesse_b
    Mar 27 at 14:42












  • Positive. Yes. That's all I have in my test script besides shebang.

    – StevieD
    Mar 27 at 14:49













0












0








0


0






I'm on a mac, bash version 5.0.2.



Got this:



 v="ksdjfksjkjsdf"
v2=$v::-6


Get this:



-bash: -6: substring expression < 0









share|improve this question














I'm on a mac, bash version 5.0.2.



Got this:



 v="ksdjfksjkjsdf"
v2=$v::-6


Get this:



-bash: -6: substring expression < 0






bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 14:27









StevieDStevieD

1659




1659




closed as off-topic by Jesse_b, ilkkachu, terdon Mar 27 at 19:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by Jesse_b, ilkkachu, terdon Mar 27 at 19:51


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
If this question can be reworded to fit the rules in the help center, please edit the question.












  • Which version of bash? I just tried this with 4.4.12 and it worked as expected.

    – Panki
    Mar 27 at 14:30











  • at the top of the post: 5.0.2

    – StevieD
    Mar 27 at 14:31











  • Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried v2=$v::-14 it would

    – Jesse_b
    Mar 27 at 14:42












  • Positive. Yes. That's all I have in my test script besides shebang.

    – StevieD
    Mar 27 at 14:49

















  • Which version of bash? I just tried this with 4.4.12 and it worked as expected.

    – Panki
    Mar 27 at 14:30











  • at the top of the post: 5.0.2

    – StevieD
    Mar 27 at 14:31











  • Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried v2=$v::-14 it would

    – Jesse_b
    Mar 27 at 14:42












  • Positive. Yes. That's all I have in my test script besides shebang.

    – StevieD
    Mar 27 at 14:49
















Which version of bash? I just tried this with 4.4.12 and it worked as expected.

– Panki
Mar 27 at 14:30





Which version of bash? I just tried this with 4.4.12 and it worked as expected.

– Panki
Mar 27 at 14:30













at the top of the post: 5.0.2

– StevieD
Mar 27 at 14:31





at the top of the post: 5.0.2

– StevieD
Mar 27 at 14:31













Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried v2=$v::-14 it would

– Jesse_b
Mar 27 at 14:42






Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried v2=$v::-14 it would

– Jesse_b
Mar 27 at 14:42














Positive. Yes. That's all I have in my test script besides shebang.

– StevieD
Mar 27 at 14:49





Positive. Yes. That's all I have in my test script besides shebang.

– StevieD
Mar 27 at 14:49










4 Answers
4






active

oldest

votes


















1














Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?






share|improve this answer























  • Turns out my bash --version returns different value than /bin/bash --version. OP was running with #!/bin/bash version.

    – StevieD
    Mar 27 at 14:59


















3














Using negative lengths was added in Bash 4.2:




$PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha




And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:



$ s=123456; echo "$s::-2"
1234
$ s=123456; echo "$s::-8"
bash: -8: substring expression < 0


That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.






share|improve this answer






























    -1














    This worked:



    v="aksdjfkajsdkfjakdf"
    length=$#v
    v2=$v:0: length - 6





    share|improve this answer























    • This will not work if the length of v is less than 6 characters, which is likely your issue in the first place.

      – Jesse_b
      Mar 27 at 14:49











    • I know it's more than 6 characters in the code it will be used in.

      – StevieD
      Mar 27 at 14:50











    • Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.

      – StevieD
      Mar 27 at 14:51











    • You should file a bug report with bash

      – Jesse_b
      Mar 27 at 14:52











    • I have no idea if it's a bug or not. Could work differently in bash 5.

      – StevieD
      Mar 27 at 14:52


















    -1














    I have done by below method



    v="ksdjfksjkjsdf"
    echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"


    output



     echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"

    ksdjfks





    share|improve this answer























    • Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or sed, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed.)

      – Stephen Kitt
      Mar 28 at 8:23

















    4 Answers
    4






    active

    oldest

    votes








    4 Answers
    4






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?






    share|improve this answer























    • Turns out my bash --version returns different value than /bin/bash --version. OP was running with #!/bin/bash version.

      – StevieD
      Mar 27 at 14:59















    1














    Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?






    share|improve this answer























    • Turns out my bash --version returns different value than /bin/bash --version. OP was running with #!/bin/bash version.

      – StevieD
      Mar 27 at 14:59













    1












    1








    1







    Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?






    share|improve this answer













    Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 27 at 14:38









    Joni LJoni L

    361




    361












    • Turns out my bash --version returns different value than /bin/bash --version. OP was running with #!/bin/bash version.

      – StevieD
      Mar 27 at 14:59

















    • Turns out my bash --version returns different value than /bin/bash --version. OP was running with #!/bin/bash version.

      – StevieD
      Mar 27 at 14:59
















    Turns out my bash --version returns different value than /bin/bash --version. OP was running with #!/bin/bash version.

    – StevieD
    Mar 27 at 14:59





    Turns out my bash --version returns different value than /bin/bash --version. OP was running with #!/bin/bash version.

    – StevieD
    Mar 27 at 14:59













    3














    Using negative lengths was added in Bash 4.2:




    $PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha




    And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:



    $ s=123456; echo "$s::-2"
    1234
    $ s=123456; echo "$s::-8"
    bash: -8: substring expression < 0


    That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.






    share|improve this answer



























      3














      Using negative lengths was added in Bash 4.2:




      $PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha




      And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:



      $ s=123456; echo "$s::-2"
      1234
      $ s=123456; echo "$s::-8"
      bash: -8: substring expression < 0


      That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.






      share|improve this answer

























        3












        3








        3







        Using negative lengths was added in Bash 4.2:




        $PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha




        And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:



        $ s=123456; echo "$s::-2"
        1234
        $ s=123456; echo "$s::-8"
        bash: -8: substring expression < 0


        That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.






        share|improve this answer













        Using negative lengths was added in Bash 4.2:




        $PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha




        And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:



        $ s=123456; echo "$s::-2"
        1234
        $ s=123456; echo "$s::-8"
        bash: -8: substring expression < 0


        That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 15:04









        ilkkachuilkkachu

        63.2k10104181




        63.2k10104181





















            -1














            This worked:



            v="aksdjfkajsdkfjakdf"
            length=$#v
            v2=$v:0: length - 6





            share|improve this answer























            • This will not work if the length of v is less than 6 characters, which is likely your issue in the first place.

              – Jesse_b
              Mar 27 at 14:49











            • I know it's more than 6 characters in the code it will be used in.

              – StevieD
              Mar 27 at 14:50











            • Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.

              – StevieD
              Mar 27 at 14:51











            • You should file a bug report with bash

              – Jesse_b
              Mar 27 at 14:52











            • I have no idea if it's a bug or not. Could work differently in bash 5.

              – StevieD
              Mar 27 at 14:52















            -1














            This worked:



            v="aksdjfkajsdkfjakdf"
            length=$#v
            v2=$v:0: length - 6





            share|improve this answer























            • This will not work if the length of v is less than 6 characters, which is likely your issue in the first place.

              – Jesse_b
              Mar 27 at 14:49











            • I know it's more than 6 characters in the code it will be used in.

              – StevieD
              Mar 27 at 14:50











            • Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.

              – StevieD
              Mar 27 at 14:51











            • You should file a bug report with bash

              – Jesse_b
              Mar 27 at 14:52











            • I have no idea if it's a bug or not. Could work differently in bash 5.

              – StevieD
              Mar 27 at 14:52













            -1












            -1








            -1







            This worked:



            v="aksdjfkajsdkfjakdf"
            length=$#v
            v2=$v:0: length - 6





            share|improve this answer













            This worked:



            v="aksdjfkajsdkfjakdf"
            length=$#v
            v2=$v:0: length - 6






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 14:47









            StevieDStevieD

            1659




            1659












            • This will not work if the length of v is less than 6 characters, which is likely your issue in the first place.

              – Jesse_b
              Mar 27 at 14:49











            • I know it's more than 6 characters in the code it will be used in.

              – StevieD
              Mar 27 at 14:50











            • Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.

              – StevieD
              Mar 27 at 14:51











            • You should file a bug report with bash

              – Jesse_b
              Mar 27 at 14:52











            • I have no idea if it's a bug or not. Could work differently in bash 5.

              – StevieD
              Mar 27 at 14:52

















            • This will not work if the length of v is less than 6 characters, which is likely your issue in the first place.

              – Jesse_b
              Mar 27 at 14:49











            • I know it's more than 6 characters in the code it will be used in.

              – StevieD
              Mar 27 at 14:50











            • Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.

              – StevieD
              Mar 27 at 14:51











            • You should file a bug report with bash

              – Jesse_b
              Mar 27 at 14:52











            • I have no idea if it's a bug or not. Could work differently in bash 5.

              – StevieD
              Mar 27 at 14:52
















            This will not work if the length of v is less than 6 characters, which is likely your issue in the first place.

            – Jesse_b
            Mar 27 at 14:49





            This will not work if the length of v is less than 6 characters, which is likely your issue in the first place.

            – Jesse_b
            Mar 27 at 14:49













            I know it's more than 6 characters in the code it will be used in.

            – StevieD
            Mar 27 at 14:50





            I know it's more than 6 characters in the code it will be used in.

            – StevieD
            Mar 27 at 14:50













            Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.

            – StevieD
            Mar 27 at 14:51





            Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.

            – StevieD
            Mar 27 at 14:51













            You should file a bug report with bash

            – Jesse_b
            Mar 27 at 14:52





            You should file a bug report with bash

            – Jesse_b
            Mar 27 at 14:52













            I have no idea if it's a bug or not. Could work differently in bash 5.

            – StevieD
            Mar 27 at 14:52





            I have no idea if it's a bug or not. Could work differently in bash 5.

            – StevieD
            Mar 27 at 14:52











            -1














            I have done by below method



            v="ksdjfksjkjsdf"
            echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"


            output



             echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"

            ksdjfks





            share|improve this answer























            • Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or sed, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed.)

              – Stephen Kitt
              Mar 28 at 8:23















            -1














            I have done by below method



            v="ksdjfksjkjsdf"
            echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"


            output



             echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"

            ksdjfks





            share|improve this answer























            • Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or sed, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed.)

              – Stephen Kitt
              Mar 28 at 8:23













            -1












            -1








            -1







            I have done by below method



            v="ksdjfksjkjsdf"
            echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"


            output



             echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"

            ksdjfks





            share|improve this answer













            I have done by below method



            v="ksdjfksjkjsdf"
            echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"


            output



             echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"

            ksdjfks






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 18:02









            Praveen Kumar BSPraveen Kumar BS

            1,7391311




            1,7391311












            • Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or sed, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed.)

              – Stephen Kitt
              Mar 28 at 8:23

















            • Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or sed, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed.)

              – Stephen Kitt
              Mar 28 at 8:23
















            Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or sed, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed.)

            – Stephen Kitt
            Mar 28 at 8:23





            Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or sed, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed.)

            – Stephen Kitt
            Mar 28 at 8:23



            -bash

            Popular posts from this blog

            Frič See also Navigation menuinternal link

            Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant

            fontconfig warning: “/etc/fonts/fonts.conf”, line 100: unknown “element blank” The 2019 Stack Overflow Developer Survey Results Are In“tar: unrecognized option --warning” during 'apt-get install'How to fix Fontconfig errorHow do I figure out which font file is chosen for a system generic font alias?Why are some apt-get-installed fonts being ignored by fc-list, xfontsel, etc?Reload settings in /etc/fonts/conf.dTaking 30 seconds longer to boot after upgrade from jessie to stretchHow to match multiple font names with a single <match> element?Adding a custom font to fontconfigRemoving fonts from fontconfig <match> resultsBroken fonts after upgrading Firefox ESR to latest Firefox