Syntax error Using expr substr in bash script
I have written small bash script to try to get the last x characters of a string.
#!/bin/bash
string="This is my string. I want THIS TEXT"
echo -e "string: $string"
length=${#string}
echo "length: $length"
start=$(($length-9))
echo "start: $start"
text=`expr substr $string $start 9`
echo "text: $text"
exit 0
The output is giving me a "syntax error":
string: This is my string. I want THIS TEXT
length: 35
start: 26
expr: syntax error
text:
I'm sure it's fairly simple but I can't figure it out. Can anybody point out开发者_如何学运维 what I'm doing wrong? This is being run on SunOS.
how about this?
echo ${string:(-4)}
where -4 is your offset from the end
精彩评论