How can I connect my Mac to my iPhone hotspot via USB from the command line? About a mont ago, my Mac's wifi stopped working for unknown reasons. I was recommended to do a clean install, but I need internet connection to run the installer from the recovery menu, so I want to get to the internet by using the iPhone mobile data.
Is there a way to connect to the internet by sharing the iPhone data over USB?
I know I can reinstall from an USB drive, but I've already erased the main HD and I'm stuck here.
If you’ve already erased your hard drive then there’s no “command line” to use to begin with.  You need a minimal operating system in which to boot into and it sounds like you don’t have that.
Here are a few options for you to move forward:

Get a USB Ethernet or USB WiFi dongle (I suggest Ethernet) to connect your Mac directly into your router/modem so you can do the Internet Recovery.

Obtain a USB installer from a trusted source (friend/colleague)

Put your Mac in Target Disk Mode and using another the other Mac, install macOS onto that machine. Alternatively, if the drive in your Mac is removable and you have the correct USB enclosure/adapter connect it to another Mac and install macOS onto it.


Regarding your WiFi....
Reinstalling an OS, regardless if it’s macOS, Windows, Linux, etc. is the absolutely last thing you should try.  It’s a “Hail Mary pass” type of move because the person diagnosing the problem is out of ideas.  It rarely solves the problem because if it does start working again, you haven’t identified what caused the issue to begin with to prevent it in the future.
In your case, resintalling won’t fix it because you’re not in macOS anywhere and you still cannot connect to WiFi for recovery.  If it doesn’t work in this “bare minimum mode“ it’s not going to work when the full OS is loaded.  The thing is, you didn’t have to erase the drive to find this out.  All you had to do was boot into Recovery to test it out.
I wrote this not as an admonishment, but as a cautionary tale to you and future readers.  If you’re being told, especially initially that “the fix is to resinstall” stop and look elsewhere; seek better guidance.  I’m not saying that it’s never the answer; I’m saying it shouldn’t be the first go to.
This is the equivalent to having a plumbing or electrical issue in your house and the contractor tells you that you need to demolish and rebuild the whole house  to fix it.  In this case, normally it would be ridiculous  to do that, but if the entire house had sub-standard or dangerous wiring (like wrapped in asbestos), demolishing might make sense.  It should be your last option after everything else is attempted/evaluated.