Shell Metacharacters and Operators: Filename Expansion (wildcards *, ?, []), Input/Output Redirection (>, >>, <), Pipes (|), Command Substitution ($(...))

Shell कुछ characters को specially treat करता है: asterisk किसी भी filenames के लिए खड़ा है, greater-than sign output को एक file में भेजता है, pipe एक command को अगली में feed करता है, और dollar-parentheses एक command का output ठीक आपकी line में drop कर देता है।

12 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Superpowers वाले Characters

कुछ characters shell के लिए कुछ special मतलब रखते हैं। जब आप * type करते हैं, shell literally star नाम की एक file नहीं ढूँढता, यह इसे filenames match करने के लिए expand करता है। जब आप > type करते हैं, आप एक greater-than sign print नहीं कर रहे, आप output को एक file में redirect कर रहे हैं।

ये special characters metacharacters (और operators) कहलाते हैं, और ये plain commands को powerful one-liners में बदल देते हैं। यह lesson essentials cover करता है: wildcards, redirection, pipes, और command substitution। इन्हें master कीजिए और command line dramatically ज़्यादा capable बन जाती है।

At a glance

SymbolMeaning
*Wildcard: filenames में कितने भी characters match करता है
?Wildcard: exactly एक character match करता है
> fileOutput को file में INTO redirect करता है (इसे overwrite करता है)
>> fileOutput redirect करता है, file में APPENDING करते हुए
< fileFile से input लेता है
cmd1 | cmd2Pipe: cmd1 का output cmd2 का input बनता है
$(cmd)Command substitution: cmd के output से replace होता है

Practical

Work में Metacharacters (verified)

$ ls *.txt              # wildcard: every name ending in .txt
a1.txt  a2.txt  out.txt
$ ls ?1.*              # ? = one char: names like a1.xxx, b1.xxx
a1.txt  b1.log

$ echo "line1" > out.txt    # > creates/overwrites out.txt
$ echo "line2" >> out.txt   # >> appends a second line
$ wc -l < out.txt           # < feeds the file in as input
2

$ echo "There are $(ls *.txt | wc -l) txt files"   # pipe + substitution
There are 3 txt files

Formula

Redirection Files में जाता है; Pipes Commands में जाते हैं

दो ideas अलग रखिए। Redirection एक command को एक file से connect करता है: > और >> output को एक file में भेजते हैं, < एक file से input पढ़ता है। एक pipe | एक command को दूसरी command से connect करता है: यह पहली का output सीधे दूसरी में feed करता है, बीच में कोई file नहीं।

तो sort names.txt > sorted.txt sorted output को एक file में डालता है, जबकि sort names.txt | uniq sorted output को uniq में feed करता है। एक तरफ़ files, दूसरी तरफ़ commands: यह distinction ज़्यादातर confusion untangle करती है।

Quiz

आप चलाते हैं: echo "first" > log.txt , फिर बाद में echo "second" > log.txt (दोनों single > के साथ)। log.txt में क्या है?

  1. दोनों lines: first और second
  2. सिर्फ़ 'second', क्योंकि एक single > हर बार file overwrite करता है
  3. सिर्फ़ 'first', क्योंकि file पहले से create हो चुकी थी
  4. कुछ नहीं; > file delete कर देता है
Show the answer

सिर्फ़ 'second', क्योंकि एक single > हर बार file overwrite करता है

एक single > हर बार file के contents OVERWRITE करता है। पहली command log.txt में 'first' डालती है; दूसरी command इसे पूरी तरह 'second' से overwrite कर देती है, तो सिर्फ़ 'second' बचता है। Option A सिर्फ़ तब correct होता अगर दूसरा redirect >> (append) इस्तेमाल करता, जो बिना erase किए add करता है, यही crucial difference है। Option C उल्टा है: newer write older content को replace करता है, दूसरी तरफ़ नहीं। Option D ग़लत है: > नया content लिखता है (और ज़रूरत पड़ने पर file create करता है); यह इसे यहाँ empty नहीं छोड़ता। याद रखने वाला trap: > overwrite करता है (अगर आप add करना चाहते थे तो data loss), >> append करता है। Existing content रखने के बारे में unsure होने पर, >> इस्तेमाल कीजिए।

Think first

> और >> को Confuse करना इतना Dangerous Mistake क्यों है?

दोनों एक file में लिखते हैं। Experienced users single > को इतनी real caution से क्यों treat करते हैं? फिर tap कीजिए।

Show the answer

क्योंकि एक single > file में जो भी पहले से था उसे SILENTLY DESTROY कर देता है, कोई warning नहीं और कोई undo नहीं। Imagine कीजिए आपके पास एक log file है जो हफ़्तों से important results collect कर रही है, और आप एक और line add करना चाहते हैं। अगर आप command >> log.txt type करें आप APPEND करते हैं, और सारे पुराने results safely रखे रहते हैं। पर अगर आप slip करके single > के साथ command > log.txt type कर दें, shell पहले file को empty तक TRUNCATE करता है, फिर नया content लिखता है, तो हफ़्तों के results एक instant में गायब हो जाते हैं, और इन्हें recover करने के लिए कोई recycle bin नहीं है (rm का lesson याद कीजिए: Linux destructive actions को soften नहीं करता)। Danger यह है कि दोनों operators लगभग identical दिखते हैं, एक character अलग, फिर भी एक preserve करता है और एक obliterate करता है। यही वजह है careful users हर बार pause करने की habit build करते हैं जब वे एक matter करने वाली file में > type करते हैं, और जब भी वे replace की बजाय add करने का इरादा रखते हैं >> पर default करते हैं। Single > fresh output create करने के लिए perfect है, पर एक ऐसी file पर aimed जिसकी आप care करते हैं, यह एक loaded footgun है। एक extra > का मतलब है 'जो है वो रखो'; इसे भूलना का मतलब है 'सब कुछ erase करो'।

Summary

Key takeaways

  • Metacharacters वे characters हैं जिन्हें shell specially treat करता है, simple commands को powerful one-liners में बदलते हुए।
  • Wildcards filenames expand करते हैं: * कोई भी characters match करता है, ? exactly एक match करता है, [ ] एक set match करता है।
  • Redirection एक command को एक file से connect करता है: > overwrite करता है, >> append करता है, < एक file से input पढ़ता है।
  • एक pipe | एक command को दूसरी command से connect करता है, बीच में कोई file के बिना output सीधे feed करते हुए।
  • Command substitution $(cmd) उस command के output से replace होता है, इसे आपकी line में drop करते हुए।
  • Danger: एक single > overwrite करता है (existing content destroy करते हुए) जबकि >> append करता है; add करते समय >> पर default कीजिए।
  • Memory hook: * कई, ? एक, > overwrite, >> append, | एक command को pipe, $(...) output insert।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Shell Scripting in Linux

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Shell Metacharacters and Operators: Filename Expansion (wildcards *, ?, []), Input/Output Redirection (>, >>, <), Pipes (|), Command Substitution ($(...)) · Linux Operating System (LOS) (Minor-04) · Gri-Learn