In its simplest form this could be a bungee cord attached at both ends to the wall with hooks on top of an existing book shelf.
This is in the bathroom and the ledge above the wainscotting is narrow; an old towel rack served the purpose of holding books perfectly.
I had been using stretchy cord but on more than one occasion it had let one of the books loose and that was startling. A bungee cord would have had more ooomph to it but I found this rack before I found an interesting looking bungee cord.