0Pricing
Linux Command Line Mastery · บทเรียน

ตัวแปรสภาพแวดล้อมและนามแฝง

จัดการและปรับแต่งสภาพแวดล้อมเชลล์ด้วยตัวแปรสภาพแวดล้อมและนามแฝงของคำสั่ง

ตัวแปรสภาพแวดล้อมและนามแฝง เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 $HOME

Important 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 type ls, 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 shell

Making 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
alias

Check 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 $VAR to see a variable's value and printenv to list them all.
  • export makes 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวแปรสภาพแวดล้อมและนามแฝง”

จัดการและปรับแต่งสภาพแวดล้อมเชลล์ด้วยตัวแปรสภาพแวดล้อมและนามแฝงของคำสั่ง คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัวแปรสภาพแวดล้อมและนามแฝง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม

ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐาน Git จากบรรทัดคำสั่ง
  2. ตัวแปรสภาพแวดล้อมและนามแฝง
  3. การกำหนดค่าเชลล์: `.bashrc`, `.zshrc`, `.profile`
  4. การแตกแขนงและผสานด้วย Git จากบรรทัดคำสั่ง
← กลับไปที่ Linux Command Line Mastery