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

ตัวแปร อินพุต และเอาต์พุต

เรียนรู้การประกาศตัวแปร รับอินพุตจากผู้ใช้ และจัดการเอาต์พุตของสคริปต์ในสคริปต์เชลล์

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What are Script Variables?

In shell scripting, variables are like containers for storing data. They help make your scripts flexible and reusable.

Instead of hardcoding values, you can use variables to hold information such as filenames, user inputs, or temporary results.

Declaring & Assigning Variables

To create a variable, you simply assign a value to a name. No special keyword is needed!

  • Use NAME="Coddy" (no spaces around =).
  • Values with spaces usually need quotes (single or double).
  • Numbers don't strictly need quotes, but it's often good practice for consistency.

Accessing Variable Values

To use the value stored in a variable, you prepend its name with a $ sign.

For example, if you declared GREETING="Hello", you'd use $GREETING to get "Hello". You can also use ${VARIABLE}, which is clearer in complex situations.

Variable Naming Tips

Follow these simple rules for variable names:

  • Start with a letter or underscore.
  • Can contain letters, numbers, and underscores.
  • Avoid using special shell keywords.
  • Common practice: use uppercase for global/environment variables, lowercase for script-local ones.

Demo: Using Variables

Let's see a simple script that declares and uses a variable.

Notice how $MESSAGE retrieves the stored text.

#!/bin/bash

# Declare a variable
MY_NAME="Coddy"
GREETING="Hello"

# Access and print variables
echo "$GREETING, $MY_NAME!"

# Update the variable
MY_NAME="Learner"
echo "Nice to meet you, $MY_NAME!"

Getting User Input: `read`

The read command is essential for making your scripts interactive. It waits for the user to type something and press Enter.

  • read VARIABLE: Stores input into VARIABLE.
  • read -p "Prompt: " VARIABLE: Displays a prompt before waiting for input.

Demo: Taking User Input

This script uses read -p to ask for your name and then greets you personally.

Try running it and entering your name!

#!/bin/bash

# Prompt for user's name and store it
read -p "What's your name? " USER_NAME

# Greet the user
echo "Hello, $USER_NAME! Welcome to CoddyKit."

Script Output: `echo` & `printf`

You've seen echo for basic output. It simply prints its arguments followed by a newline.

For more control, printf (like in C) allows formatted output, specifying data types and alignment. It doesn't add a newline automatically, so use \n.

Demo: Input, Output & Variables

This example combines variables, user input with read, and formatted output using printf.

It calculates a simple sum based on your input.

#!/bin/bash

# Declare a base number
BASE_NUM=10

# Get a number from the user
read -p "Enter another number: " USER_NUM

# Calculate the sum
# Use $((...)) for arithmetic operations
SUM=$((BASE_NUM + USER_NUM))

# Print the result using printf for formatting
printf "The sum of %d and %d is %d.\n" "$BASE_NUM" "$USER_NUM" "$SUM"

Quick Check

Consider the following shell script snippet:

#!/bin/bash
NAME="Alice"
read -p "Enter a city: " CITY
echo "Hello, $NAME from $CITY!"

If the user types London and presses Enter, what will be the final output?

Recap: Variables, Input, Output

Great job! You've learned the fundamentals of shell scripting:

  • Variables store data using VAR="value".
  • Access variables with $VAR or ${VAR}.
  • read gets user input, often with -p for a prompt.
  • echo prints simple messages.
  • printf offers formatted output.

These are building blocks for dynamic and interactive scripts!

คำถามที่พบบ่อย

บทเรียน “ตัวแปร อินพุต และเอาต์พุต” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวแปร อินพุต และเอาต์พุต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัวแปร อินพุต และเอาต์พุต” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ตัวแปร อินพุต และเอาต์พุต
  2. คำสั่งเงื่อนไข: `if`, `else`, `case`
  3. ลูป: `for`, `while`, `until`
  4. ฟังก์ชันและอาร์กิวเมนต์ในสคริปต์เชลล์
← กลับไปที่ Linux Command Line Mastery