Is there a way to get the Docker Hub username of the current user?

Update for modern Docker.

Seems that docker info no longer contains username. I have instead learned to extract it from the credential store via jq. I haven't tried this on every credStore, but for the ones I have checked on macOS and Linux, this works.

# line breaks added for readability; this also works as a oneliner
docker-credential-$(
  jq -r .credsStore ~/.docker/config.json
) list | jq -r '
  . |
    to_entries[] |
    select(
      .key | 
      contains("docker.io")
    ) |
    last(.value)
'

[bonus] Show the entire contents of your credential helper

In this case I've settled on docker-credential-desktop, but it should work with any. You can extract your credential helper from your Docker config as I did in the previous code block.

docker-credential-desktop list | \
    jq -r 'to_entries[].key'   | \
    while read; do
        docker-credential-desktop get <<<"$REPLY";
    done

Display the username with:

docker info | sed '/Username:/!d;s/.* //'

Store it in a variable with:

username=$(docker info | sed '/Username:/!d;s/.* //'); 
echo $username

Note that if you have bash history expansion (set +H) you will be unable to put double quotes around the command substitution (e.g. "$(cmd...)" because ! gets replaced with your last command. Escaping is very tricky with these nested expansions and using it unquoted as shown above is more readable and works.