环境变量与别名
使用环境变量和命令别名管理并定制 Shell 环境。
环境变量与别名 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Your Shell's Environment
Welcome! In this lesson, we'll dive into how your Linux shell keeps track of important information. This 'environment' helps your commands run smoothly.
Think of it as your shell's personal workspace, full of settings and shortcuts.
Meet Environment Variables
Environment variables are dynamic named values that store information used by the shell and other programs. They let you customize how your system behaves.
- They are like temporary storage for your shell.
- They can hold paths, user settings, and more.
- Other programs can read these variables to know how to act.
Checking Current Variables
You can see all active environment variables using the printenv command. To view a specific variable, use echo $VARIABLE_NAME.
The dollar sign $ before a variable name tells the shell to show its value.
printenv | head -n 3
echo $HOMEImportant System Variables
Two very common and important environment variables are PATH and HOME:
PATH: A list of directories where the shell looks for executable commands. When you typels, the shell checks these directories.HOME: The absolute path to your home directory. This is where your personal files and settings are usually stored.
Setting Temporary Variables
You can create your own environment variables for the current shell session. Just assign a value to a name:
VARIABLE_NAME="value"
Note that there are no spaces around the = sign. These variables are temporary and disappear when you close the terminal.
MY_MESSAGE="Hello CoddyKit"
echo $MY_MESSAGE
# This variable only exists in this shellMaking Variables Global with 'export'
Variables set directly are only available in the current shell. To make them available to any sub-processes (like scripts or other commands you run from your current shell), you need to export them.
export GLOBAL_VAR="I am global"
bash -c 'echo $GLOBAL_VAR'
# Without export, 'bash -c' wouldn't see it.Introducing Command Aliases
Command aliases are custom shortcuts for longer commands or sequences of commands. They save you typing and can even fix common typos.
- They personalize your command-line experience.
- You define a short name that expands to a longer command.
- Great for frequently used, complex commands.
Creating Quick Shortcuts
To create an alias, use the alias command followed by name='command'. Like variables, these are temporary and last only for the current shell session.
A common example is aliasing ls -alF to ll for a detailed file listing.
alias ll='ls -alF'
ll
# Now 'll' runs 'ls -alF'Listing and Removing Aliases
To see all aliases currently active in your shell, simply type alias without any arguments. If you want to remove an alias, use the unalias command.
alias
alias c='clear'
alias
unalias c
aliasCheck Your Understanding
You've learned about environment variables and aliases. Let's test your knowledge!
Consider the following:
MY_VAR="Test"
export MY_VAR
Which statement is true after running these commands?
Lesson Summary
Great job! You've successfully explored environment variables and aliases.
- Environment variables store key-value pairs for your shell and programs.
- Use
echo $VARto see a variable's value andprintenvto list them all. exportmakes variables available to sub-processes.- Aliases are custom shortcuts for commands, created with
alias name='command'. - Both are temporary by default, lasting only for the current shell session.
Next, you'll learn how to make these customizations permanent!
常见问题解答
「环境变量与别名」课时是免费的吗?
是的 — 「环境变量与别名」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「环境变量与别名」这节课中我会学到什么?
使用环境变量和命令别名管理并定制 Shell 环境。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「环境变量与别名」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。