Hi I'm using ksh.
And i'm trying to get the substring like below.
but giving the following error
#!/bin/ksh
foo=teststring
bar=${foo:0:5}
echo $bar
And the error is
./sbstr_test.sh[4]: bar=${foo:0:5}: bad substitution
what is wrong in this script. Please correct me
---------- Post updated at 06:11 PM ---------- Previous update was at 05:21 PM ----------
Even i tried with the following but of no use
#!/bin/ksh
foo=teststring
bar=${$foo:0:5}
echo $bar
and getting the error same as above
PikK45
June 25, 2013, 8:49am
2
What are you trying to do here???
vbe
June 25, 2013, 8:49am
3
Why not simply bar="$foo:0:5" ?
Most probably you're using ksh88 (or pdksh), but the syntax above is available in ksh93.
Note that zsh and bash support the ${varname:n:m} notation too.
Edit: I suppose the OP wants to extract a substring from the value of the variable:
% foo=teststring
% printf '%s\n' "${foo:0:5}"
tests
Note that in this case you can use also:
% printf '%.5s\n' "$foo"
tests
or:
% printf '%s\n' "${foo%${foo#?????}}"
tests