The substring operation
${line:3}
would extract all characters starting at position 3, which indeed is: [ D,E,]
(I added [] just for readability).
However, in command substitution echo ${line:3}
, shell performs word splitting and removes the leading blank character, giving the result as D,E,
.
Put the substring expression in double quotes to preserve the leading blank, like this:
echo "Output-"`echo "${line:3}"` # => Output- D,E,
To understand this more clearly, try this:
line="C , D,E," # $() does command substitution like backquotes; it is a much better syntax
string1=$(echo "${line:3}") # double quotes prevent word splitting
string2=$(echo ${line:3}) # no quotes, shell does word splitting
string3="$(echo ${line:3})" # since double quotes are outside but not inside $(), shell still does word splitting
echo "string1=[$string1], string2=[$string2], string3=[$string3]"
gives the output:
string1=[ D,E,], string2=[D,E,], string3=[D,E,]
See also: