The echo command
Write arguments to the standard output. 66 examples from the lessons of the IVski Linux Playground — try them in the terminal next to them.
Examples
echo Hello, Linux!
echo 'hello' | tr 'a-z' 'A-Z'Lowercase to uppercase
echo 'Hello World' | tr ' ' '_'Replace spaces with _
echo 'hello' | tr -d 'l'Delete all 'l' chars
echo $MYNAME
echo ${MYNAME}Safer: braces avoid ambiguity
echo "Hello, ${MYNAME}!"
echo $?Prints 0
echo $PATH
echo "Hello from my first script!"
echo "Today is: $(date)"
echo "You are: $(whoami)"
echo "Backup created: $BACKUP_DIR/backup-$DATE.tar.gz"
echo ${#NAME}Length: 5
echo ${NAME,,}Lowercase: ivski
echo ${NAME^^}Uppercase: IVSKI
echo ${NAME:0:2}Substring: IV
echo ${NAME/sk/SK}Replace: IVSKi
echo ${FRUITS[0]}apple
echo ${FRUITS[@]}All elements
echo ${#FRUITS[@]}Count: 3
echo "$fruit"
echo $((A + B))13
echo $((A * B))30
echo $((A / B))3 (integer division)
echo $((A % B))1 (remainder)
echo "Hello, IVski!"
echo "Not a guest"
echo "Name is empty"
echo "Name is set"
echo "Working age"
echo "Privileged user"
echo "Starts with IVsk"
echo "Starts with uppercase"
echo "Hello, $name!"
echo "Processing: $file"
echo "Count: $i"
echo "$i"
echo "Iteration: $COUNT"
echo "Line: $line"
echo "Running..."
echo "$COUNT"
echo "Hello, $1!"
echo "$content" > "$filename"
echo "Created: $filename"
echo $(date +%Y-%m-%d)
echo "Today: $TODAY"
echo "Found it!"
echo "[$(date +%H:%M:%S)] $1"
echo "Script name: $0"
echo "First arg: $1"
echo "Second arg: $2"
echo "All args: $@"
echo "Arg count: $#"
echo "Usage: $0 <source> <destination>"
echo "Copying $SRC to $DST"
echo "Argument: $1"
echo "file1 file2 file3" | xargs ls -la
echo '<h1>Hello from my server</h1>' | sudo tee /var/www/html/index.html
echo '<h1>About us</h1>' | sudo tee /var/www/html/about.html
echo secret | sudo tee /var/www/html/secret.html
echo '<h1>Welcome to the shop</h1>' | sudo tee /var/www/shop/index.html
echo '127.0.0.1 shop.local' | sudo tee -a /etc/hosts
echo "Requests: $(wc -l < ~/logs/access.log)"$(…) puts a command's answer into the text
echo "…" >> ~/logs/health.log>> adds a line, > would replace the file
echo "Hello, $1"$1: the first word after the script's name
Lessons that use echo
- What is Linux?
- cut & tr — Extracting Columns
- Environment Variables
- The PATH Variable
- Your First Script
- Variables in Scripts
- if/else — Conditions
- Loops — for and while
- Functions
- Script Arguments
- xargs — Build Command Lines
- Serving Your Own Pages
- Configuration & Server Blocks
- Project: Daily Log Report
- Project: Server Health Check
- Project: New Teammate Script