6

I'm reading the last line of a file into a variable. Then I want to get the last X characters of the string variable:

#!/bin/bash
someline="this is the last line content"
echo ${someline}
somepart=${someline: -5}
echo ${somepart}

Run with: sh lastchars.sh

Result:

this is the last line content
line 4: Bad substitution

What might be wrong here?

membersound
  • 1,410
  • 1
    What gives the actual error? The echo? If so, please show us the contents of the $line variable. Do you also get the error if you use quotes (echo "${lastchars}")? – terdon Nov 20 '17 at 11:11
  • Your code works exactly like it's supposed to for me, please add input and full output to your question. – dessert Nov 20 '17 at 11:13
  • the ${line} content prints just fine. anyways the substitution error is shown. @terdon the echo ${lastchars} is not reached at all, because getting the last 5 characters from the variable already produces the error. – membersound Nov 20 '17 at 11:22
  • I can't reproduce what you describe. Are you sure you're using bash? Are you running this in a script or from the command line? If a script, how are you running that script? – terdon Nov 20 '17 at 11:30
  • I added the complete example above. I cannot help, still getting the error of this... – membersound Nov 20 '17 at 11:36
  • 1
    That's because you're using the wrong shell. You're not running bash at all but instead are using sh which is a different shell: dash. See my answer. – terdon Nov 20 '17 at 11:37

2 Answers2

20

It sounds like you're not using bash at all. I can only reproduce the error you show if I use dash instead of bash:

  1. bash:

    $ line="someline content"
    $ echo ${line}
    someline content
    $ lastchars=${line: -5}
    $ echo ${lastchars}
    ntent
    
  2. dash:

    $ line="someline content"
    echo ${line}
    lastchars=${line: -5}
    echo ${lastchars}
    $ someline content
    $ dash: 3: Bad substitution
    

Your shebang line is pointing to bash, but you are running the script with sh, so the shebang is ignored. /bin/sh on Ubuntu systems is actually dash, a minimal shell that doesn't support the syntax you are trying to use.

When using a shebang line, there's no reason to explicitly call a shell for the script, just make it executable (chmod a+x /path/to/script.sh) and run it without specifying an interpreter:

/path/to/script.sh

Alternatively, just use the right one:

bash /path/to/script.sh
Dan
  • 13,119
terdon
  • 100,812
1

Obviously using the build-in functions of a certain shell are nice, but you can also accomplish the task using standard UNIX commands, so it will work in any shell:

String="This is some text"
StrLen=`echo ${String} | wc -c`
From=`expr $StrLen - 5`
echo $String | cut -c${From}-${StrLen}