Trying to retrieve first 5 characters from string in bash error?

BashShell

Bash Problem Overview


I'm trying to retrieve the first 5 characters from a string and but keep getting a Bad substitution error for the string manipulation line, I have the following lines in my teststring.sh script:

TESTSTRINGONE="MOTEST"

NEWTESTSTRING=${TESTSTRINGONE:0:5}
echo ${NEWTESTSTRING}

I have went over the syntax many times and cant see what im doing wrong

Thanks

Bash Solutions


Solution 1 - Bash

Depending on your shell, you may be able to use the following syntax:

expr substr $string $position $length

So for your example:

TESTSTRINGONE="MOTEST"
echo `expr substr ${TESTSTRINGONE} 0 5`

Alternatively,

echo 'MOTEST' | cut -c1-5

or

echo 'MOTEST' | awk '{print substr($0,0,5)}'

Solution 2 - Bash

echo 'mystring' |cut -c1-5 is an alternative solution to ur problem.

more on unix cut program

Solution 3 - Bash

Works here:

$ TESTSTRINGONE="MOTEST"
$ NEWTESTSTRING=${TESTSTRINGONE:0:5}
$ echo ${NEWTESTSTRING}
MOTES

What shell are you using?

Solution 4 - Bash

Substrings with ${variablename:0:5} are a bash feature, not available in basic shells. Are you sure you're running this under bash? Check the shebang line (at the beginning of the script), and make sure it's #!/bin/bash, not #!/bin/sh. And make sure you don't run it with the sh command (i.e. sh scriptname), since that overrides the shebang.

Solution 5 - Bash

This might work for you:

 printf "%.5s" $TESTSTRINGONE

Solution 6 - Bash

Works in most shells

TESTSTRINGONE="MOTEST"
NEWTESTSTRING=${TESTSTRINGONE%"${TESTSTRINGONE#?????}"}
echo ${NEWTESTSTRING}
# MOTES

Solution 7 - Bash

You were so close! Here is the easiest solution: NEWTESTSTRING=$(echo ${TESTSTRINGONE::5})

So for your example:

$ TESTSTRINGONE="MOTEST"
$ NEWTESTSTRING=$(echo ${TESTSTRINGONE::5})
$ echo $NEWTESTSTRING
MOTES

Solution 8 - Bash

You can try sed if you like -

[jaypal:~/Temp] TESTSTRINGONE="MOTEST"
[jaypal:~/Temp] sed 's/\(.\{5\}\).*/\1/' <<< "$TESTSTRINGONE"
MOTES

Solution 9 - Bash

echo $TESTSTRINGONE|awk '{print substr($0,0,5)}'

Solution 10 - Bash

That parameter expansion should work (what version of bash do you have?)

Here's another approach:

read -n 5 NEWTESTSTRING <<< "$TESTSTRINGONE"

Solution 11 - Bash

The original syntax will work with BASH but not with DASH. On debian systems you might think you are using bash, but maybe dash instead. If /bin/dash/exist then try temporarily renaming dash to something like no.dash, and then create soft a link, aka ln -s /bin/bash /bin/dash and see if that fixes the problem.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMo.View Question on Stackoverflow
Solution 1 - BashAlex LView Answer on Stackoverflow
Solution 2 - BashBalaswamy VaddemanView Answer on Stackoverflow
Solution 3 - Bashuser647772View Answer on Stackoverflow
Solution 4 - BashGordon DavissonView Answer on Stackoverflow
Solution 5 - BashpotongView Answer on Stackoverflow
Solution 6 - BashLauriView Answer on Stackoverflow
Solution 7 - BashamcView Answer on Stackoverflow
Solution 8 - Bashjaypal singhView Answer on Stackoverflow
Solution 9 - BashVijayView Answer on Stackoverflow
Solution 10 - Bashglenn jackmanView Answer on Stackoverflow
Solution 11 - BashWileyView Answer on Stackoverflow