惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
G
Google Developers Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
博客园 - Franky
S
SegmentFault 最新的问题
Jina AI
Jina AI
爱范儿
爱范儿
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
C
Check Point Blog
月光博客
月光博客
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Zsh Startup Files in macOS
2023-06-17 · via jdhao's digital space

In this post, I want to share what I find about the Zsh startup files and their loading order in macOS.

login and interactive shell#

There are a lot of posts discussing what a login and interactive shell is. These two aspects are independent.

In different mode, zsh will source different startup files when it is started. In Zsh, to test if the shell is login shell, we can test login option:

if [[ -o login ]]; then
    echo 'yes'
else
    echo 'no'
fi

You can also use echo $0 and check if the first character in output is -.

You can exit login/non-login shell using exit. For the login shell, logout also works (it does not work for non-login shell).

wezterm#

In wezterm, every time we open a new tab, it will by default start a login shell using the $SHELL env variable. The way it does this is via prefixing argv0 with - when spawning the shell.

It is a standard way in Linux to check if the shell is a login shell. You can use echo $0 and check if the first character is -.

ref:

Zsh startup file source order#

According to man zsh (the section about STARTUP/SHUTDOWN FILES), the loading order of startup file is:

/etc/zshenv
$ZDOTDIR/.zshenv

if login_shell:
    /etc/zprofile
    $ZDOTDIR/.zprofile
endif

if interactive_shell:
    /etc/zshrc
    $ZDOTDIR/.zshrc
endif

if login_shell:
    /etc/zlogin
    $ZDOTDIR/.zlogin
endif

When the shell exit or logout, then the following script:

/etc/zlogout
$ZDOTDIR/.zlogout

For the login file, you can add something that is best done once for the login. For example, showing some greetings.

For the log out file, you can add some snippet to save the logout time:

logfile="$HOME/logoutfile"

if [[ -f $logfile ]]
then
  echo "logging out: $(date)" >> $logfile
else
  echo "logging out: $(date)" > $logfile
fi

These config files do not have to exist in the system at all. When you know their loading order, you can then customize based on your preference.

On macOS, the file /etc/zprofile is especially evil, it run a utility called path_helper, which changes the PATH variable in a weird way. You can also my other posts for the details.

ref: