0Pricing
Linux Command Line Mastery · Lekcja

Zmienne, wejście i wyjście

Nauczą się Państwo deklarować zmienne, przyjmować dane wejściowe od użytkownika i zarządzać wyjściem skryptów powłoki.

Zmienne, wejście i wyjście to bezpłatna lekcja Linux Command Line Mastery na CoddyKit. To lekcja 1 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Linux Command Line Mastery, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Linux Command Line Mastery zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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!

Często zadawane pytania

Czy lekcja „Zmienne, wejście i wyjście” jest bezpłatna?

Tak — pełny tekst „Zmienne, wejście i wyjście” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Linux Command Line Mastery, przejdź na CoddyKit PRO. Kurs Linux Command Line Mastery zawiera 4 lekcji w sumie.

Co nauczysz się w „Zmienne, wejście i wyjście”?

Nauczą się Państwo deklarować zmienne, przyjmować dane wejściowe od użytkownika i zarządzać wyjściem skryptów powłoki. Ćwiczysz Linux Command Line Mastery z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Linux Command Line Mastery?

Nie wymagamy żadnego doświadczenia. Linux Command Line Mastery w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 1 z 4.

Ile czasu zajmuje lekcja „Zmienne, wejście i wyjście”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Linux Command Line Mastery?

Tak. Każda lekcja Linux Command Line Mastery zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Zmienne, wejście i wyjście
  2. Instrukcje warunkowe: `if`, `else`, `case`
  3. Pętle: `for`, `while`, `until`
  4. Funkcje i argumenty w skryptach powłoki
← Powrót do Linux Command Line Mastery