there many ways to do so.
str="hello world"
1) cut
Where -cN-M tells the cut command to return columns N to M, inclusive.
cut -cN-M
start=1
end=5
sub_str=`echo ${str} | cut -c${start}-${end}`
output: hello
2)
bash Note: if you are not sure of having bash
, consider using cut
. ${str:offset}
${str:offset:length}
sub_str=${str:0:5}
output: hello
3) expr
expr substr string position length
sub_str=`expr substr $str 1 5`
output: hello