Andy DeVane

October 21, 2021

Using jq to get passwords in a fancy script

So I've been playing around with jq which is a shell utility for parsing json. It's a wonderful little tool for creating scripts that interact with APIs. Here's an example: I have a script that allows me to select an account from my 1password vault, and it will copy that password to my clipboard. (I'm currently using a browser that doesn't support extentions)

 #!/bin/bash
 
 # Get password for 1password with pass and sign into 1password
 eval $(yes `pass show callrail/1password` | op signin callrail)
 
 # List 1password items in the private vault
 items=$(op list items --vault Private)
 
 # pass the item titles to dmenu and store the selection
 # (fzf could be used to run this script in a terminal for those that don't use X11)
 selection=$(echo $items | jq -r '.[] | .overview.title' | sed 's/\"//g' | dmenu)
 
 # get the password of the selected item
 password=$(op get item $selection | jq -r ".details.fields | .[] | select(.designation==\"password\") .value")
 
 # copy the selected password to the keyboard
 echo $password | xclip -sel clip
 
 # notify the user that the passsword has been copied to the keyboard
 notify-send 1password "`echo $selection` password copied to clipboard"

This was really fun to make.