Variabili, input e output
Impari a dichiarare variabili, accettare input dall’utente e gestire l’output degli script shell.
Variabili, input e output è una lezione Linux Command Line Mastery gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Linux Command Line Mastery, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Linux Command Line Mastery include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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!
Domande Frequenti
La lezione «Variabili, input e output» è gratuita?
Sì — il testo completo di «Variabili, input e output» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Linux Command Line Mastery, passa a CoddyKit PRO. Il corso Linux Command Line Mastery include 4 lezioni in totale.
Cosa imparerò in «Variabili, input e output»?
Impari a dichiarare variabili, accettare input dall’utente e gestire l’output degli script shell. Eserciti Linux Command Line Mastery con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Linux Command Line Mastery?
Non è richiesta alcuna esperienza precedente. Linux Command Line Mastery su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Variabili, input e output»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Linux Command Line Mastery?
Sì. Ogni lezione Linux Command Line Mastery include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Variabili, input e output
- Istruzioni condizionali: `if`, `else`, `case`
- Cicli: `for`, `while`, `until`
- Funzioni e argomenti negli script di shell