Değişkenler, Girdi ve Çıktı
Kabuk betiklerinde değişken tanımlamayı, kullanıcı girdisi almayı ve betik çıktısını yönetmeyi öğrenin.
Değişkenler, Girdi ve Çıktı, CoddyKit'te ücretsiz bir Linux Command Line Mastery dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Linux Command Line Mastery öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Linux Command Line Mastery kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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!
Sıkça Sorulan Sorular
“Değişkenler, Girdi ve Çıktı” dersi ücretsiz mi?
Evet — “Değişkenler, Girdi ve Çıktı” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Linux Command Line Mastery kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Linux Command Line Mastery kursu toplamda 4 dersten oluşur.
“Değişkenler, Girdi ve Çıktı” dersinde ne öğreneceğim?
Kabuk betiklerinde değişken tanımlamayı, kullanıcı girdisi almayı ve betik çıktısını yönetmeyi öğrenin. Linux Command Line Mastery ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Linux Command Line Mastery öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Linux Command Line Mastery, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.
“Değişkenler, Girdi ve Çıktı” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Linux Command Line Mastery dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Linux Command Line Mastery dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Değişkenler, Girdi ve Çıktı
- Koşullu İfadeler: `if`, `else`, `case`
- Döngüler: `for`, `while`, `until`
- Kabuk Betiklerinde İşlevler ve Bağımsız Değişkenler