← Back to Bash Course | Chapter 1: Setup & Basics | Lesson 10 of 12

echo और printf

echo और printf दो मुख्य तरीके हैं जिनसे एक Bash script screen पर text display करती है, printf format पर कहीं ज़्यादा precise control देता है।
Syntax
bash
echo "text"
printf "format string\n" arguments

Basic echo

echo text print करने का सबसे simple तरीका है: यह अपने arguments को single spaces से separate करके standard output पर लिखता है और एक trailing newline जोड़ता है। Multiple arguments एक single space से जुड़ते हैं, चाहे source में उन्हें कितने भी spaces से separate किया गया हो।

उदाहरण: Basic echo

bash
#!/bin/bash
echo "Hello, Bash!"
echo Hello    Bash

echo Flags: -n और -e

echo -n trailing newline के बिना text print करता है, जब आप piece by piece एक line build करना चाहें तो useful है। echo -e backslash escape sequences जैसे \n (newline) और \t (tab) के interpretation को on करता है, जो otherwise literally print होते हैं।

Warning: Plain echo "a\nb" (बिना -e के) literal characters \n print करता है, कोई असली newline नहीं।

उदाहरण: echo Flags: -n and -e

bash
#!/bin/bash
echo -n "No newline here... "
echo "and this continues on the same line"
echo -e "Column1\tColumn2\nRow2Col1\tRow2Col2"

printf Basics

printf अपने पहले argument के रूप में एक format string लेता है और बाकी arguments को %s (string) या %d (integer) जैसे placeholders में substitute करता है।

echo के विपरीत, यह कभी अपने आप एक newline नहीं जोड़ता, इसलिए जहां भी आपको एक चाहिए वहां आप explicitly \n लिखते हैं।

उदाहरण: printf Basics

bash
#!/bin/bash
printf "Name: %s, Age: %d\n" "Ada" 36
printf "Pi is roughly %.2f\n" 3.14159

Repeated/Formatted Output के लिए printf

printf अपने format string को extra arguments पर recycle करता है अगर आप इसे placeholders से ज़्यादा values दें, जो एक loop के बिना एक consistent format में lists print करने के लिए handy है।

यह printf को echo से ज़्यादा predictable भी बनाता है जब print हो रहा data special characters रख सकता हो।

उदाहरण: printf for Repeated/Formatted Output

bash
#!/bin/bash
printf "Item: %s\n" apple banana cherry
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि echo हर जगह identically behave करता है; backslash escapes और -e जैसे flags की इसकी handling shells के बीच और यहां तक कि Bash versions/builtins बनाम /bin/echo के बीच भी अलग होती है।
  2. यह भूल जाना कि printf automatically एक newline नहीं जोड़ता, echo के विपरीत, इसलिए consecutive printf calls से output एक ही line पर run together हो सकता है।
  3. User-controlled या variable data print करने के लिए echo उपयोग करना जो एक dash से शुरू हो सकता है (जैसे -n), जिसे echo text के बजाय एक flag के रूप में misinterpret कर सकता है।
चैप्टर सारांश
  • echo default रूप से अपने arguments को एक newline के साथ print करता है।
  • echo -n trailing newline suppress करता है; echo -e \n और \t जैसे backslash escapes के interpretation को enable करता है।
  • printf automatically कभी newline नहीं जोड़ता -- आपको format string में खुद \n शामिल करना होता है।
  • printf %s, %d, और %.2f जैसे format specifiers support करता है, C के printf जैसा, precise control देते हुए जो echo के पास नहीं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.