Q:

Bash how to replace comment backslash with nothing

I have a strign in bash with the following format:
// comment. 
I want to obtain a new variable with comment alone (no backslash) and I don't want to depend on the // begin the first two characters in the string.  How can I do this? 
I have tried this:
  nline=${line/%/////}
  echo $nline

To use string substitution but it doesn't work. 

A:

Perhaps you want the # substitution? 
$ a='// this is a comment'
$ printf "%s\n" "${a#// }"
this is a comment
$ a='not a comment'
$ printf "%s\n" "${a#// }"
not a comment

And as SergA pointed out, a little better patterns for our variable extraction can save us the need for the sed solution below:
$ a="first //a comment"
$ printf "%s" "${a##*//}"

If you just want to get the comment part of a line anywhere it is you could use sed like so:
$ a="first //a comment"
$ printf "%s\n" "$a" | sed -e 's,^.*// \?,,' 
a comment

which of course you could store in another variable:
nline=$(printf "%s" "$a" | sed -e 's,^.*// \?,,') 

(note also that I remved the \n from the printf)

