How to run PlatformIO serial monitor from the command line

First, activate the PlatformIO virtual environment which will give you access to the pio script:

source ~/.platformio/penv/bin/activate

Now, if you have – for example – an environment called esp32dev listed in platformio.ini:

platform = espressif32
board = esp32dev
framework = arduino

you can build & upload using the following code:

pio run -e esp32dev -t monitor

You can also combine both commands (the virtual env activation and the pio run command) into a single line of shell script:

source ~/.platformio/penv/bin/activate && pio run -e esp32dev -t monitor