how to compile arduino sketch from termnial and then upload the firmware

You can use arduino-cli to upload, just like how the Arduino IDE does it. You can find details here:

In the near future, the Arduino IDE is going to use arduino-cli for installing hardware packages, installing libraries, compiling, uploading. At that point, the IDE will only be a graphical interface for the arduino-cli. So arduino-cli will do anything the Arduino IDE can do, and more. This is something Arduino has been working towards for years. It used to be that everything was crammed into the IDE's Java code but they've gradually been moving it out into separate tools. That's extremely helpful for people like you who want to use a different IDE or not use an IDE at all.