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:

run_pio_monitor.sh
source ~/.platformio/penv/bin/activate

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

platformio_env_example.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino

you can build & upload using the following code:

run_pio_monitor_cmd.sh
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:

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

Check out similar posts by category: PlatformIO