You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让fish-shell使用GNU coreutils的printf功能?

在Fish Shell中替换内置printf为GNU coreutils版本的方法

问题说明

在Fish shell执行以下命令时会报错:

❯ printf '%q\n' 'André Previn & London Symphony Orchestra'
%q: invalid conversion specification

原因是Fish自带的内置printf不支持GNU coreutils printf特有的%q格式符。而MacOS默认使用BSD衍生的工具,即使在Fish中尝试调用系统原生的printf(用builtin或command前缀),也会因为BSD printf不支持%q而失败:

# zsh中可正常运行
❯ printf '%q\n' 'test'
test

# Fish中调用内置或系统printf均报错
❯ builtin printf '%q\n' 'test'
%q: invalid conversion specification

❯ command printf '%q\n' 'test'
printf: illegal format character q

解决步骤

  • 安装GNU coreutils:Mac上通过Homebrew安装GNU工具集,执行:
    brew install coreutils
    
    安装完成后,GNU版本的printf会以gprintf的名称存在。
  • 直接调用GNU版本:用gprintf替代原生printf即可使用%q格式符,示例:
    gprintf '%q\n' 'André Previn & London Symphony Orchestra'
    
  • 设置别名替换默认printf:如果希望Fish中输入printf时默认调用GNU版本,编辑Fish配置文件~/.config/fish/config.fish,添加:
    alias printf='gprintf'
    
    保存后执行source ~/.config/fish/config.fish使配置生效,后续直接使用printf就能调用GNU版本的功能。

内容的提问来源于stack exchange,提问作者Drew Daniels

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 06:22:11