2

When a substring having a space in the beginning is printed the leading whitespace is removed.

$ line="C , D,E,";
$ echo "Output-`echo ${line:3}`";
Output-D,E,

Why is the leading whitespace being removed from the output and how can I print the whitespace?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Nitish
  • 1,686
  • 8
  • 23
  • 42

2 Answers2

4

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:

codeforester
  • 39,467
  • 16
  • 112
  • 140
-1

Shell does not preserve multiple spaces between arguments, so the two spaces collapse to one.

echo "Output-`echo ${line:3}`";

Evaluates to:

echo "Output-`echo  -D,E,`";

And:

echo  -D,E,

Evaluates to:

-D,E,

In other words echo receives the same arguments for both of those calls:

echo  -D,E,
echo -D,E,

As the argument arrive as an array of Strings, end each String is already trimmed (no spaces around it).

Piotr
  • 504
  • 4
  • 6
  • 1
    These spaces are only "between arguments" if the string is being split into multiple words (subsequently passed as separate arguments) at all. Correctly quoting the expansion would prevent that from being the case. – Charles Duffy Mar 22 '18 at 03:02