Use STDERR for errors
When using the cli, the "Vault is locked." message is output to STDOUT instead of STDERR. This makes scripting/aliasing more difficult.
For my case, I am trying to use an alias to quickly copy a password to my clipboard for use elsewhere.
alias mypass="bw get password mypass | pbcopy"
It works fine when the Vault is unlocked, but swallows the error output and pipes it to pbcopy, silently waiting to be a headache for me. :)
$ mypass
$ # no output makes me think it worked
However, when I paste later, I find that 'Vault is locked.' is in my clipboard.
More testing to verify the error message is going to STDOUT:
$ bw get password foo 2>/dev/null
Vault is locked. # this should be gone, but its going to STDOUT instead of STDERR
Having the error message(s) go to STDERR will let me know there was a problem, even if I was piping output to pbcopy or the like.
Further testing makes this issue a little more wide spread on other errors.
I have this alias too:
alias bwu='export BW_SESSION="$(bw unlock --raw)"'
When I ran it while unknowingly logged out, it prompted for password and displayed no error message. This is because the error message is getting set to BW_SESSION.
$ bwu
? Master password: [hidden]
# no stderr output
$ echo $BW_SESSION
Invalid master password.
$ bw get password mypass | pbcopy
# no stderr output
# clipboard now contains the message "Session key is invalid."
Should be fixed with https://github.com/bitwarden/cli/commit/f6bb228249319d8a40dd33aa40734733e931e2c7
By the way, you can pre-check if your vault is locked with bw unlock --check