変数、入力、出力
シェルスクリプトで変数を宣言し、ユーザー入力を受け取り、スクリプトの出力を管理する方法を学びます。
「変数、入力、出力」はCoddyKit上の無料Linux Command Line Masteryレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 intoVARIABLE.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
$VARor${VAR}. readgets user input, often with-pfor a prompt.echoprints simple messages.printfoffers formatted output.
These are building blocks for dynamic and interactive scripts!
よくある質問
「変数、入力、出力」レッスンは無料ですか?
はい。「変数、入力、出力」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Linux Command Line Masteryコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Linux Command Line Masteryコースには全4レッスンが含まれています。
「変数、入力、出力」で何を学びますか?
シェルスクリプトで変数を宣言し、ユーザー入力を受け取り、スクリプトの出力を管理する方法を学びます。 ブラウザで直接実行するハンズオンコードでLinux Command Line Masteryを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Linux Command Line Masteryを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのLinux Command Line Masteryは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「変数、入力、出力」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このLinux Command Line Masteryレッスンでコードを書いて実行できますか?
はい。すべてのLinux Command Line Masteryレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。