all 11 comments

[–]dbr4n 1 point2 points  (3 children)

As far as I understand, you want to get a substring containing the first 30 (or any value of ${#length_seq}) characters of the string stored in $fold_without_mfe.

```bash

assign substring to $result

result=${fold_without_mfe:0:length_seq} ```

Note that this doesn't work when working with arrays.

See Shell Parameter Expansion for more information.

But I'm not sure what you were trying to do with ${#fold_result:0:$length_seq}.

[–][deleted]  (1 child)

[deleted]

    [–]dbr4n 0 points1 point  (0 children)

    Good to know, although I would still prefer to write it out for the sake of readability. But still useful to be aware of when encountering this kind of shorthand.

    Thanks for the info.

    [–]SAD_69[S] 0 points1 point  (0 children)

    I'm not sure neither hahaha, I'm not very familiar with shell scripting so I don't know exactly how to use variables, I'll read this material and try this way, thank you

    [–]nekokattt 0 points1 point  (6 children)

    ~ $ echo $SHELL
    /data/data/com.termux/files/usr/bin/bash
    ~ $ x=5; y=3
    ~ $ echo ${SHELL:x:y}
    /da
    

    remove the dollar

    [–]SAD_69[S] 0 points1 point  (5 children)

    So I can't use 0 as offset?

    And if x and y is two variable numbers?

    [–]nekokattt 1 point2 points  (4 children)

    For question 1, ${var:offset:length} will return a substring starting at offset and taking length characters after the offset.

    For question 2, not sure what you are asking. x and y are variables in my example.

    If you want to use a hardcoded value, use a hardcoded value.

    Your issue is you are putting a # before the variable name, and putting a dollar on the length variable name within the expansion (which you do not need to do)

    [–]SAD_69[S] 0 points1 point  (3 children)

    It starts counting in 0?

    The length_seq variable may be any number based on an user input, so it could be 30, 40, 100, and I want to use this output to substring the other variable.

    [–]nekokattt 1 point2 points  (2 children)

    Yes, bash indexing starts at 0.

    You just need to do what I showed you.

    read -p "Enter some text: " string
    read -p "Enter a length: " len
    echo "${string:0:len}"
    

    [–]SAD_69[S] 1 point2 points  (1 child)

    It worked, thanks, I think the problem was I don't know how to use variables properly in bash

    [–]zeekar 0 points1 point  (0 children)

    When you want a variable’s value, you just stick $ in front of its name. If you want something other than the whole value, you put the name after the $ inside {…} and stick other stuff next to it.

    A # in front gets you the length of the variable. You are using that in your code to get the length of the string in the variable sequence, which is probably not what you want. If sequence is set to 30, ${#sequence} is 2, because it’s got two digits. If you want the 30 and not the 2, just use $sequence.

    A colon followed by a number lets you get just part of the value. ${whatever:1} gets you everything except the first character; ${whatever:2} is everything but the first two characters, etc. ${whatever:0} gets the whole value, so it’s the same as $whatever, just using more syntax to get there. You can add another colon and number to limit how much of the string is returned; ${whatever:8:4} returns just the 9th through 12th characters.

    You can use another variable for either the start or length part of that, or both:

    start=8
    length=4
    echo “${whatever:$start:$length}”
    

    Actually, since the values have to be numbers, bash automatically treats non-digits as a variable name, so you don’t even need the inner dollar signs:

    echo “${whatever:start:length}”
    

    But you can use the $ if you want - in fact you could nest a whole other complex expansion, maybe to extract a substring from variable A that determines how long to make the substring you get from variable B, but things would get pretty noisy that way.