How to temporarily switch profiles for AWS CLI?

For AWS CLI v1, the cleanest solution is:

export AWS_DEFAULT_PROFILE=user2

Afterward, commands like:

aws s3 ls

... are handled from the appropriate account.

For AWS CLI v2, the following will work:

export AWS_PROFILE=user2

The accepted answer assumes you are using a Linux or Mac terminal. I added commands for Windows, Linux and Mac OS.

Windows

CMD

set AWS_PROFILE=profile_name

Powershell

$env:AWS_PROFILE = 'profile_name'

Linux or Mac

export AWS_PROFILE=profile_name

These will set your aws profile that you will use every time you execute an aws command. But if you just want to switch profile temporarily for one aws command.

aws [command] [sub-command] --profile [profile-name]