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

如何在fish终端记录命令与输出?config.fish配置问题求解

Fixing Your Fish Shell Logging Script

Let's break down why your original script isn't working and fix it step by step:

What's Wrong with the Original Code?

  1. Incorrect variable assignment: Using env now= date +%Y%m%d%H%M%S doesn't capture the date output into the now variable—it just runs date with an empty now environment variable, which isn't what you want.
  2. Missing command substitution: Fish requires wrapping commands in () to capture their output as a variable value. Your original code doesn't do this for the date command.
  3. Subshell behavior issue: Running script directly in config.fish starts a nested subshell, so you'd have to exit that subshell to get back to your main fish session. That's not ideal for continuous session logging.

Corrected Script for ~/.config/fish/config.fish

Here's the fixed version that automatically logs every fish session's input and output to a timestamped file:

# Create logs directory if it doesn't exist (avoids file path errors)
mkdir -p /Users/t/logs

# Capture timestamp into a variable using fish's command substitution
set now (date +%Y%m%d%H%M%S)

# Define the full path to your log file
set log_file "/Users/t/logs/script_$now.txt"

# Replace the current shell with script to log all session activity
exec script -q -f $log_file

Key Improvements Explained

  • mkdir -p: Ensures the target logs directory exists (no errors thrown if it's already there).
  • set now (date +%Y%m%d%H%M%S): Properly captures the date command's output into the now variable using fish's native syntax.
  • exec script -q -f $log_file:
    • exec replaces the current fish shell with the script process, so every command you run in the session is logged directly (no nested subshell layer).
    • -q runs script in quiet mode (hides the default "Script started" and "Script done" messages from your terminal).
    • -f flushes the log file immediately, so you can view the log in real-time without waiting for buffers to write.

Alternative: Manual Logging (If You Don't Want Auto-Start)

If you prefer to start logging on demand instead of auto-starting with every session, add this function to your config.fish instead:

function start-log
    mkdir -p /Users/t/logs
    set now (date +%Y%m%d%H%M%S)
    set log_file "/Users/t/logs/script_$now.txt"
    exec script -q -f $log_file
end

Then just run start-log in your terminal whenever you want to begin logging.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:11:36