KandZ – Tuts

We like to help…!

Linux CLI 55 🐧 shell scripts output text

a - echo in shell scripts
echo command in shell scripting is a fundamental tool for outputting text or variables
echo "Hello, World!" → prints Hello World
echo $greeting → prints the content of $greeting variable
you can use escape characters as well
echo "This is a newline:\nAnd this is a tab:\t." → using tab escape character
-n option avoids new line
echo -n "Hello, " → avoids new line

b - printf and here document in shell scripts
printf offers more precise control over the output format
printf "Name: %s\nAge: %d\n" "$name" "$age" → formating text with printf
printf "%.2f\n" "$number" → Output will be "123.46"
Here documents are a way to pass multi-line text input into commands or scripts.
This is particularly useful for passing large blocks of text or SQL queries.
example 1 → multiline example
example 2 → use of variables example

Leave a Reply