How to transfer music from iphone to itunes?

I have all my music on my iPhone and none of it on my new Mac. I'm worried that if I try and synch my iPhone to the empty iTunes it will erase all my pre existing music from my phone to match the iTunes library.

As long as you're signed into the same Apple ID on both your iPhone and the Mac, you should be fine.

You can use another software through itunes

As long as the music is linked with your itunes account you should be fine. When you plug the iphone into your computer make sure that the manually sync music button is selected.

It will erase the music from your phone if you try to sync it. Apple "hides" the folders that the music is stored in. With the older iPods, like the classic, you could view the hidden folders and copy it over to your computer. But now it's different and the only way I know of to copy the music over is to SSH into your phone (google how, the Answer guidelines have been iffy in the past about explaining how to do it), locating the music files, and then copying it over.

The music is linked with your itunes account you should be fine. When you plug the iphone into your computer make sure that the manually sync music button is selected.