25

When I put export GPG_TTY=$(tty) in my .zshrc and restart terminal window and execute

echo $GPG_TTY

it says not a tty.

When I source .zshrc by

source ~/.zshrc && echo $GPG_TTY

it correctly reports /dev/pts/1.

What could be that my .zshrc fails to find tty when its documentation says that .zshrc is used for interactive shell initialisation?

Here is my .zshrc contents:

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.

if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

export ZSH="/home/ashar/.oh-my-zsh"
export EDITOR=nvim
export GPG_TTY=$(tty)

ZSH_THEME="powerlevel10k/powerlevel10k"

plugins=(git zsh-autosuggestions)
source $ZSH/oh-my-zsh.sh

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

2 Answers 2

76

tty command requires that stdin is attached to a terminal. When using Powerlevel10k, stdin is redirected from /dev/null when Instant Prompt is activated and until Zsh is fully initialized. This is explained in more detail in Powerlevel10k FAQ.

To solve this problem you can either move export GPG_TTY=$(tty) to the top of ~/.zshrc so that it executes before Instant Prompt is activated, or (better!) use export GPG_TTY=$TTY. The latter version will work anywhere and it's over 1000 times faster. TTY is a special parameter set by Zsh very early during initialization. It gives you access to the terminal even when stdin might be redirected.

1
  • Oddly using export GPG_TTY=$TTY didn't work for me, I had to move it to the top as well. Seemed to just produce a null value. Feb 26 at 0:55
6

I solved it so, just sharing it with others.

I moved the export GPG_TTY=$(tty) to top. Specifically above the p10k instant prompt initialization. I don't know but if I export tty after that script is executed the tty is not available. Anyways, I am not an expert in the field but it just solves the issue for me and I hope it does for you too.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .