0Pricing
Linux Command Line Mastery · Lektion

Variablen, Eingabe und Ausgabe

Lernen Sie, Variablen zu deklarieren, Benutzereingaben entgegenzunehmen und die Skriptausgabe in Shell-Skripten zu verwalten.

Variablen, Eingabe und Ausgabe ist eine kostenlose Linux Command Line Mastery-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Linux Command Line Mastery-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Linux Command Line Mastery-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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!

Häufig gestellte Fragen

Ist die Lektion „Variablen, Eingabe und Ausgabe“ kostenlos?

Ja — der vollständige Text von „Variablen, Eingabe und Ausgabe“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Linux Command Line Mastery-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Linux Command Line Mastery-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Variablen, Eingabe und Ausgabe“?

Lernen Sie, Variablen zu deklarieren, Benutzereingaben entgegenzunehmen und die Skriptausgabe in Shell-Skripten zu verwalten. Du übst Linux Command Line Mastery mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Linux Command Line Mastery zu starten?

Keine Vorkenntnisse erforderlich. Linux Command Line Mastery auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Variablen, Eingabe und Ausgabe“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Linux Command Line Mastery-Lektion Code schreiben und ausführen?

Ja. Jede Linux Command Line Mastery-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Variablen, Eingabe und Ausgabe
  2. Bedingte Anweisungen: `if`, `else`, `case`
  3. Schleifen: `for`, `while`, `until`
  4. Funktionen und Argumente in Shell-Skripten
← Zurück zu Linux Command Line Mastery