Prefixes और Suffixes हटाना
In this page:
${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 ##
#!/bin/bash
path="/usr/local/bin/tool"
echo "Shortest prefix removed: ${path#*/}"
echo "Longest prefix removed: ${path##*/}"
Login to try C/C++/Java/PHP code in the editor
% और %% के साथ एक 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 %%
#!/bin/bash
file="archive.tar.gz"
echo "Shortest suffix removed: ${file%.*}"
echo "Longest suffix removed: ${file%%.*}"
Login to try C/C++/Java/PHP code in the editor
एक File Extension निकालना
##*. combine करना (last dot तक longest-prefix removal) एक file की सिर्फ extension निकालने का standard idiom है, क्योंकि यह final . के through सब कुछ हटा देता है। यह multiple dots वाली filenames के लिए भी correctly काम करता है।
उदाहरण: Extracting a File Extension
#!/bin/bash
filename="my.report.final.pdf"
extension=${filename##*.}
echo "Extension: $extension"
Login to try C/C++/Java/PHP code in the editor
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
#!/bin/bash
filename="my.report.final.pdf"
base=${filename%.*}
echo "Base name: $base"
Login to try C/C++/Java/PHP code in the editor
#/##(front से हटाना) को%/%%(back से हटाना) से confuse करना; एक memory trick है#एक comment की शुरुआत (front) जैसा दिखता है,%आमतौर पर percentages/endings के लिए उपयोग होता है।- Single बनाम double forms को mix up करना;
#/%*shortest* matching prefix/suffix हटाते हैं, जबकि##/%%*longest* matching वाला हटाते हैं -- यह बहुत मायने रखता है जब pattern कई lengths पर match कर सके। - यह मान लेना कि ये 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 में उपयोग होता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: