echo और printf
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
#!/bin/bash
echo "Hello, Bash!"
echo Hello Bash
Login to try C/C++/Java/PHP code in the editor
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 होते हैं।
echo "a\nb" (बिना -e के) literal characters \n print करता है, कोई असली newline नहीं।उदाहरण: echo Flags: -n and -e
#!/bin/bash
echo -n "No newline here... "
echo "and this continues on the same line"
echo -e "Column1\tColumn2\nRow2Col1\tRow2Col2"
Login to try C/C++/Java/PHP code in the editor
printf Basics
printf अपने पहले argument के रूप में एक format string लेता है और बाकी arguments को %s (string) या %d (integer) जैसे placeholders में substitute करता है।
echo के विपरीत, यह कभी अपने आप एक newline नहीं जोड़ता, इसलिए जहां भी आपको एक चाहिए वहां आप explicitly \n लिखते हैं।
उदाहरण: printf Basics
#!/bin/bash
printf "Name: %s, Age: %d\n" "Ada" 36
printf "Pi is roughly %.2f\n" 3.14159
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
printf "Item: %s\n" apple banana cherry
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
echoहर जगह identically behave करता है; backslash escapes और-eजैसे flags की इसकी handling shells के बीच और यहां तक कि Bash versions/builtins बनाम/bin/echoके बीच भी अलग होती है। - यह भूल जाना कि
printfautomatically एक newline नहीं जोड़ता,echoके विपरीत, इसलिए consecutiveprintfcalls से output एक ही line पर run together हो सकता है। - User-controlled या variable data print करने के लिए
echoउपयोग करना जो एक dash से शुरू हो सकता है (जैसे-n), जिसेechotext के बजाय एक flag के रूप में misinterpret कर सकता है।
echodefault रूप से अपने arguments को एक newline के साथ print करता है।echo -ntrailing newline suppress करता है;echo -e\nऔर\tजैसे backslash escapes के interpretation को enable करता है।printfautomatically कभी newline नहीं जोड़ता -- आपको format string में खुद\nशामिल करना होता है।printf%s,%d, और%.2fजैसे format specifiers support करता है, C के printf जैसा, precise control देते हुए जोechoके पास नहीं।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: