@curleb - I have stored my string with leading spaces in a variable and when i try to use the command provided by you above...it returns a blank....i mean it does not return an output at all -
value=" no rows selected"
val=$value|sed 's/ no/no/g'
echo output=$val
This returns output as follows-
output=
which necessarily means its returning a blank output.
That gives the desired result with that specific value of $x, but it is generally wrong.
The parameter expansion pattern "# *" will only match one leading space, not more. The use of a single "#" means that the wildcard is useless; the shortest match will be selected and that match will always be a single space, if any. If you had used "##", it would've matched all characters (including non-spaces) that followed a leading space, effectively deleting the entire string. Remember, in sh globs, * is not a repeater that acts on the previous character, as it is in regular expressions; it can match an arbitrary length of arbitrary characters.
Also, the unquoted parameter expansion will convert any contiguous sequences of IFS characters (by default, spaces, tabs, and newlines) into a single space. Nothing in the problem statement indicates that such a conversion is desirable.
That is not a good solution. It removes more than just leading spaces. See above.