玩转 Shell 表情符号
Fun with Shell Emojis

原始链接: https://www.lasantha.org/blog/fun-with-shell-emojis/

## 为你的终端添加一些乐趣:随机表情符号 本文演示了一种简单的方法,通过在终端输出中添加随机表情符号,来个性化你在 Linux、macOS 和 Windows 上的 shell 体验。目标?用一点点愉悦的色彩打破日常工作的单调。 核心思想是创建一个函数 (`rand_emoji`),从预定义的列表中随机选择一个表情符号。实现方式略有不同,具体取决于你的 shell:Bash (Linux)、Zsh (macOS) 和 PowerShell (Windows) 需要针对数组索引(0 索引与 1 索引)和变量作用域进行调整。 一旦添加到你的 shell 配置文件 (`.bashrc`、`.zshrc` 或 `$PROFILE`),重新加载配置文件就可以使用该函数。例如,`echo "Hi $(rand_emoji)"` 将每次打印一个带有不同表情符号的问候语。 除了简单的问候语,该函数还可以集成到 shell 提示符、脚本输出,甚至 Git 提交钩子中,以获得更具吸引力和视觉吸引力的终端体验。这是一个快速、低成本的定制,可以提升士气并鼓励进一步探索 shell 脚本。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 用 Shell 表情符号玩乐 (lasantha.org) 4 分,kiriberty 发表于 1 小时前 | 隐藏 | 过去 | 收藏 | 1 条评论 kiriberty 发表于 1 小时前 [–] 你的 shell 环境不必枯燥。我们可以用随机的表情符号装饰 shell 的用户消息。这是一个小技巧,有助于打破日常终端工作的单调。 让我带你了解如何在 Linux、macOS 和 Windows 上设置它。 回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Lasantha Kularatne posts shell terminal bash zsh powershell fun

This article is nothing technical. I want to show the fun side of shell development.

Your shell environment doesn't need to be boring. We can decorate the shell's user messages with fun emojis that are random each time. It's a small touch that helps break the monotony of everyday terminal work.

Let me walk you through how to set this up on Linux, macOS, and Windows.

Linux (Debian/Ubuntu)

Shell environment: Bash

Add the following function to your ~/.bashrc:

rand_emoji() {
  local imgs=("😊" "👻" "😽" "😺" "😌" "🙃" "😃" "🎂" "😎" "🤗" "😈" "🤡" "😻" "💩" "🤓" "🥳" "🤩" "🤑" "🙀" "😱" "🙈" "🧙" "🦄" "🧚" "🤖" "🐶" "🥂" "🍭" "🍿" "🎉" "🎊" "🕺" "🏮" "🎏" "🪔" "🔮" "🏆")

  # Bash arrays are 0-based by default.
  # $RANDOM % length returns a number from 0 to length-1.
  local img_id=$(( RANDOM % ${#imgs[@]} ))

  echo "${imgs[$img_id]:-🙂}"
}

How it works

  • local imgs=(...) — Declares a local array of emoji strings. Using local keeps the variable scoped to the function so it doesn't leak into your shell session.
  • ${#imgs[@]} — Returns the total number of elements in the array.
  • $RANDOM — A built-in Bash variable that produces a random integer between 0 and 32767 each time it's referenced.
  • $(( RANDOM % ${#imgs[@]} )) — The modulo (%) operation constrains the random number to a valid array index (0 to length-1), since Bash arrays are zero-based.
  • ${imgs[$img_id]:-🙂} — Looks up the emoji at the random index. The :- syntax is a fallback — if the value is empty or unset for any reason, it defaults to 🙂.

macOS

Shell environment: Zsh

Add the following function to your ~/.zshrc:

rand_emoji() {
  local imgs=("😊" "👻" "😽" "😺" "😌" "🙃" "😃" "🎂" "😎" "🤗" "😈" "🤡" "😻" "💩" "🤓" "🥳" "🤩" "🤑" "🙀" "😱" "🙈" "🧙" "🦄" "🧚" "🤖" "🐶" "🥂" "🍭" "🍿" "🎉" "🎊" "🕺" "🏮" "🎏" "🪔" "🔮" "🏆")

  # Zsh arrays are 1-based.
  # We add +1 so the range becomes 1 to Length (instead of 0 to Length-1)
  local img_id=$(( ($RANDOM % ${#imgs[@]}) + 1 ))

  echo "${imgs[$img_id]:-🙂}"
}

How it works

  • local imgs=(...) — Same as Bash, declares a local array of emojis.
  • ${#imgs[@]} — Returns the array length, just like in Bash.
  • $RANDOM — Works the same way as in Bash, producing a random integer.
  • $(( ($RANDOM % ${#imgs[@]}) + 1 )) — This is where Zsh differs. Zsh arrays are 1-based (the first element is at index 1, not 0). The modulo gives us 0 to length-1, so we add 1 to shift the range to 1 through length — matching Zsh's indexing.
  • ${imgs[$img_id]:-🙂} — Looks up the emoji at the random index. The :- syntax is a fallback — if the value is empty or unset for any reason, it defaults to 🙂.

Windows

Shell environment: PowerShell

Add the following function to your PowerShell profile ($PROFILE):

function rand_emoji {
	$emojis = @('😊', '👻', '😽', '😺', '😌', '🙃', '😃', '🎂', '😎', '🤗', '😈', '🤡', '😻', '💩', '🤓', '🥳', '🤩', '🤑', '🙀', '😱', '🙈', '🧙', '🦄', '🧚', '🤖', '🐶', '🥂', '🍭', '🍿', '🎉', '🎊', '🕺', '🏮', '🎏', '🪔', '🔮', '🏆')
	return $emojis[(Get-Random -Minimum 0 -Maximum $emojis.Count)]
}

How it works

  • $emojis = @(...) — Creates a PowerShell array of emoji strings. The @() syntax explicitly defines an array. Unlike Bash/Zsh, PowerShell variables declared inside a function are local by default — no local keyword needed.
  • $emojis.Count — Returns the number of elements in the array. PowerShell arrays are zero-based, like Bash.
  • Get-Random -Minimum 0 -Maximum $emojis.Count — PowerShell's built-in cmdlet for generating random numbers. The -Minimum is inclusive and -Maximum is exclusive, so this produces a value from 0 to length-1 — exactly what we need for a zero-based index.
  • $emojis[...] — Standard array indexing to retrieve the emoji at the random position.

Try it out

Once you've added the function to your shell config, reload it (or open a new terminal) and run:

echo "Hi $(rand_emoji)"

Each time you run it, you'll get a different greeting — Hi 🦄, Hi 💩, Hi 🙃, and so on.

What can you do with this?

Here are a few ways to put rand_emoji to use:

  • Customize your shell prompt — Prepend a random emoji to your prompt string (PS1 in Bash/Zsh, prompt function in PowerShell) so every new command line feels a little different.
  • Liven up script output — Sprinkle rand_emoji into your build scripts, deployment tools, or CI logs to make status messages more glanceable and less wall-of-text.
  • Git commit hooks — Add an emoji to your pre-commit or post-commit messages for a quick visual cue in your log history.

Why bother?

  • It reduces monotony — Staring at a plain terminal all day gets old. A small, random visual change keeps things just interesting enough to stay engaged.
  • It's a low-effort morale boost — It takes two minutes to set up and costs nothing. A tiny 💩 or 🦄 popping up in your prompt can genuinely make you smile mid-debugging-session.
  • It's a gateway to shell customization — Once you start tweaking your shell config for fun, you'll naturally learn more about how your environment works — arrays, variables, random numbers, profile scripts — all useful knowledge.

Make your shell environment more fun -- Try it!


联系我们 contact @ memedata.com