0Pricing
Linux Command Line Mastery · 강의

변수, 입력 및 출력

셸 스크립트에서 변수를 선언하고 사용자 입력을 받고 스크립트 출력을 관리하는 방법을 배웁니다.

변수, 입력 및 출력은(는) CoddyKit의 무료 Linux Command Line Mastery 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 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!

자주 묻는 질문

“변수, 입력 및 출력” 강의는 무료인가요?

네 — “변수, 입력 및 출력” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Linux Command Line Mastery 강의 전체를 잠금 해제할 수 있습니다. Linux Command Line Mastery 강의에는 총 4개의 강의가 포함되어 있습니다.

“변수, 입력 및 출력”에서 뭘 배우나요?

셸 스크립트에서 변수를 선언하고 사용자 입력을 받고 스크립트 출력을 관리하는 방법을 배웁니다. 브라우저에서 직접 실행하는 실습 코드로 Linux Command Line Mastery을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Linux Command Line Mastery을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Linux Command Line Mastery은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“변수, 입력 및 출력” 강의는 얼마나 걸리나요?

대부분의 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(으)로 돌아가기