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

Bash世界时钟脚本时区失效:所有地区均显示GMT时间求助

问题原因与解决方案

你的问题出在时区变量前的多余斜杠上。TZ环境变量的正确格式不需要开头的/,比如应该用Europe/Berlin而不是/Europe/Berlin,带斜杠的写法会让系统无法识别时区,最终 fallback 到GMT时间。

修改脚本里的时区定义数组,去掉每个值的前导斜杠即可:

#!/bin/bash

# Define time zones - 去掉时区路径前的斜杠
declare -A timezones
timezones["Germany"]="Europe/Berlin"
timezones["United_States_New_York"]="America/New_York"
timezones["Japan"]="Asia/Tokyo"

# Use fzf to select multiple countries
selected_countries=$(printf '%s\n' "${!timezones[@]}" | fzf -m)

while true; do
    buffer=$( 
    # Iterate over selected countries
    for selected_country in $selected_countries; do
        timezone="${timezones[$selected_country]}"
        echo "The date in ${selected_country} is $(env TZ=$timezone date) $timezone"
    done
)
    clear  
    echo "$buffer"
    sleep 1

done

修改后运行脚本,选中的时区就会显示正确的本地时间了——德国会显示中欧时间,日本显示东京时间,不会再统一用GMT。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 18:49:50