← Back to Bash Course | Chapter 7: String Manipulation | Lesson 6 of 10

Prefixes और Suffixes हटाना

Bash के पास एक matching prefix या suffix को string से strip करने के लिए dedicated parameter expansions हैं, जो exactly वह तरीका है जिससे आप एक filename को इसके directory path या extension से अलग करते हैं।
Syntax
bash
${variable#prefix_pattern}     # shortest prefix
${variable##prefix_pattern}    # longest prefix
${variable%suffix_pattern}     # shortest suffix
${variable%%suffix_pattern}    # longest suffix

# और ## के साथ एक Prefix हटाना

${s#pattern} s के start पर सबसे shortest text strip करता है जो pattern से match करता है, जबकि ${s##pattern} सबसे longest match strip करता है।

यह difference तब मायने रखता है जब pattern varying amounts of text से match कर सके, जैसे एक wildcard के बाद एक fixed delimiter।

उदाहरण: Removing a Prefix with # and ##

bash
#!/bin/bash
path="/usr/local/bin/tool"
echo "Shortest prefix removed: ${path#*/}"
echo "Longest prefix removed: ${path##*/}"

% और %% के साथ एक Suffix हटाना

${s%pattern} s के end पर सबसे shortest text strip करता है जो pattern से match करता है, और ${s%%pattern} सबसे longest match strip करता है। यह prefix operators को mirror करता है लेकिन इसके बजाय string के end से काम करता है।

उदाहरण: Removing a Suffix with % and %%

bash
#!/bin/bash
file="archive.tar.gz"
echo "Shortest suffix removed: ${file%.*}"
echo "Longest suffix removed: ${file%%.*}"

एक File Extension निकालना

##*. combine करना (last dot तक longest-prefix removal) एक file की सिर्फ extension निकालने का standard idiom है, क्योंकि यह final . के through सब कुछ हटा देता है। यह multiple dots वाली filenames के लिए भी correctly काम करता है।

उदाहरण: Extracting a File Extension

bash
#!/bin/bash
filename="my.report.final.pdf"
extension=${filename##*.}
echo "Extension: $extension"

Extension के बिना एक Base Name निकालना

${filename%.*} shortest suffix को हटाता है जो .* (एक dot followed by कुछ भी) से match करे, जो सिर्फ final extension को strip करता है बाकी filename को, किसी भी earlier dots सहित, intact रखते हुए।

यह extension extraction का counterpart है।

उदाहरण: Extracting a Base Name Without Extension

bash
#!/bin/bash
filename="my.report.final.pdf"
base=${filename%.*}
echo "Base name: $base"
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. #/## (front से हटाना) को %/%% (back से हटाना) से confuse करना; एक memory trick है # एक comment की शुरुआत (front) जैसा दिखता है, % आमतौर पर percentages/endings के लिए उपयोग होता है।
  2. Single बनाम double forms को mix up करना; #/% *shortest* matching prefix/suffix हटाते हैं, जबकि ##/%% *longest* matching वाला हटाते हैं -- यह बहुत मायने रखता है जब pattern कई lengths पर match कर सके।
  3. यह मान लेना कि ये operators regex उपयोग करते हैं; दूसरे pattern-based expansions की तरह, वे glob wildcards (*, ?, [...]) उपयोग करते हैं, पूरी regular expressions नहीं।
चैप्टर सारांश
  • ${s#pattern} s के start से pattern का shortest match हटाता है; ${s##pattern} start से longest match हटाता है।
  • ${s%pattern} s के end से pattern का shortest match हटाता है; ${s%%pattern} end से longest match हटाता है।
  • एक classic उपयोग है एक filename का extension निकालना (${file##*.}) या इसका base name बिना extension के (${file%.*})।
  • ये glob patterns पर operate करते हैं, वही wildcard syntax जो case और filename expansion में उपयोग होता है।

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.