{"id":2427,"date":"2025-11-21T16:23:28","date_gmt":"2025-11-21T08:23:28","guid":{"rendered":"https:\/\/www.madbull.site\/?p=2427"},"modified":"2025-11-21T16:23:28","modified_gmt":"2025-11-21T08:23:28","slug":"shell-%e4%b8%ad%e4%bd%bf%e7%94%a8-%e8%bf%9b%e8%a1%8c%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%88%aa%e5%8f%96","status":"publish","type":"post","link":"https:\/\/www.madbull.site\/?p=2427","title":{"rendered":"Shell \u4e2d\u4f7f\u7528 ${} \u8fdb\u884c\u5b57\u7b26\u4e32\u622a\u53d6"},"content":{"rendered":"\n<p>\u5728 Bash\uff08\u53ca\u5176\u4ed6 POSIX \u517c\u5bb9 shell\uff09\u4e2d\uff0c<code>${}<\/code> \u4e0d\u4ec5\u7528\u4e8e\u53d8\u91cf\u5f15\u7528\uff0c\u8fd8\u652f\u6301\u4e30\u5bcc\u7684**\u53c2\u6570\u6269\u5c55\uff08Parameter Expansion\uff09**\u529f\u80fd\uff0c\u53ef\u7528\u4e8e\u5b57\u7b26\u4e32\u622a\u53d6\u3001\u66ff\u6362\u3001\u9ed8\u8ba4\u503c\u8bbe\u7f6e\u7b49\u64cd\u4f5c\u3002\u5176\u4e2d\uff0c<strong>\u5b57\u7b26\u4e32\u622a\u53d6<\/strong>\u662f\u6700\u5e38\u7528\u7684\u529f\u80fd\u4e4b\u4e00\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u6309\u4f4d\u7f6e\u548c\u957f\u5ea6\u622a\u53d6\uff08\u57fa\u4e8e\u7d22\u5f15\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>${variable:offset}\n${variable:offset:length}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>variable<\/code>\uff1a\u8981\u64cd\u4f5c\u7684\u5b57\u7b26\u4e32\u53d8\u91cf\uff1b<\/li>\n\n\n\n<li><code>offset<\/code>\uff1a\u8d77\u59cb\u4f4d\u7f6e\uff08\u4ece 0 \u5f00\u59cb\uff09\uff1b<\/li>\n\n\n\n<li><code>length<\/code>\uff1a\u622a\u53d6\u957f\u5ea6\uff08\u53ef\u9009\uff09\uff1b<\/li>\n\n\n\n<li>\u652f\u6301\u8d1f\u6570\u504f\u79fb\uff08\u9700\u52a0\u7a7a\u683c\u6216\u62ec\u53f7\uff0c\u89c1\u4e0b\u6587\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote has-medium-font-size is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f \u6ce8\u610f\uff1a\u8be5\u8bed\u6cd5\u662f <strong>Bash \u7279\u6709<\/strong>\uff0c\u90e8\u5206 POSIX shell\uff08\u5982 dash\uff09\u4e0d\u652f\u6301\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>1.1\u3001\u6b63\u5411\u622a\u53d6\uff08\u4ece\u5de6\u5230\u53f3\uff09<\/strong><\/h2>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4ece\u6307\u5b9a\u4f4d\u7f6e\u622a\u53d6\u5230\u672b\u5c3e<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str=\"hello world\"\n\necho ${str:6}   <em># \u8f93\u51fa: world<\/em><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ece\u7d22\u5f15 <code>6<\/code>\uff08\u7b2c7\u4e2a\u5b57\u7b26\uff09\u5f00\u59cb\uff0c\u53d6\u5230\u7ed3\u5c3e\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u4ece\u6307\u5b9a\u4f4d\u7f6e\u622a\u53d6\u56fa\u5b9a\u957f\u5ea6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str=\"hello world\"\n\necho ${str:0:5} <em># \u8f93\u51fa: hello<\/em>\necho ${str:6:5} <em># \u8f93\u51fa: world<\/em><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4ece\u7d22\u5f15 <code>0<\/code> \u5f00\u59cb\uff0c\u53d6 <code>5<\/code> \u4e2a\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>1.2\u3001\u53cd\u5411\u622a\u53d6\uff08\u4ece\u53f3\u5230\u5de6\uff0c\u4f7f\u7528\u8d1f\u6570\uff09<\/strong><\/h2>\n\n\n\n<p>Bash \u652f\u6301\u8d1f\u6570\u504f\u79fb\uff0c\u8868\u793a\u4ece<strong>\u5b57\u7b26\u4e32\u672b\u5c3e<\/strong>\u5f00\u59cb\u8ba1\u7b97\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-medium-font-size\">\u2757 \u91cd\u8981\uff1a\u8d1f\u6570\u524d\u5fc5\u987b\u52a0\u7a7a\u683c\u6216\u7528\u62ec\u53f7\uff0c\u5426\u5219\u4f1a\u88ab\u89e3\u6790\u4e3a\u9ed8\u8ba4\u503c\u8bed\u6cd5 <code>${var:-value}<\/code>\u3002<\/p>\n<\/blockquote>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u6b63\u786e\u5199\u6cd5\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>str=\"hello world\"\n\n<em># \u65b9\u6cd51\uff1a\u8d1f\u6570\u524d\u52a0\u7a7a\u683c<\/em>\necho ${str: -5}     <em># \u8f93\u51fa: world<\/em>\n\n<em># \u65b9\u6cd52\uff1a\u7528\u62ec\u53f7\u5305\u88f9<\/em>\necho ${str:(-5)}    <em># \u8f93\u51fa: world<\/em>\n\n<em># \u622a\u53d6\u672b\u5c3e5\u4e2a\u5b57\u7b26\u4e2d\u7684\u524d3\u4e2a<\/em>\necho ${str: -5:3}   <em># \u8f93\u51fa: wor<\/em>\necho ${str:(-5):3}  <em># \u8f93\u51fa: wor<\/em><\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>\u9519\u8bef\u5199\u6cd5\uff08\u4f1a\u51fa\u9519\u6216\u7ed3\u679c\u4e0d\u7b26\u9884\u671f\uff09\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo ${str:-5}   <em># \u9519\uff01\u8fd9\u662f\u201c\u82e5 str \u4e3a\u7a7a\u5219\u7528 -5\u201d\u7684\u9ed8\u8ba4\u503c\u8bed\u6cd5<\/em><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u6309\u6a21\u5f0f\u5339\u914d\u622a\u53d6\uff08\u57fa\u4e8e\u901a\u914d\u7b26\uff09<\/h2>\n\n\n\n<p>\u4f7f\u7528 <code>#<\/code>\uff08\u524d\u7f00\uff09\u548c <code>%<\/code>\uff08\u540e\u7f00\uff09\u64cd\u4f5c\u7b26\uff0c\u914d\u5408\u901a\u914d\u7b26\uff08\u5982 <code>*<\/code>\uff09\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u5220\u9664\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8bed\u6cd5<\/th><th>\u4f5c\u7528<\/th><th>\u5339\u914d\u65b9\u5f0f<\/th><\/tr><\/thead><tbody><tr><td><code>${var#pattern}<\/code><\/td><td>\u5220\u9664<strong>\u6700\u77ed\u5339\u914d<\/strong>\u7684\u524d\u7f00<\/td><td>\u975e\u8d2a\u5a6a<\/td><\/tr><tr><td><code>${var##pattern}<\/code><\/td><td>\u5220\u9664<strong>\u6700\u957f\u5339\u914d<\/strong>\u7684\u524d\u7f00<\/td><td>\u8d2a\u5a6a<\/td><\/tr><tr><td><code>${var%pattern}<\/code><\/td><td>\u5220\u9664<strong>\u6700\u77ed\u5339\u914d<\/strong>\u7684\u540e\u7f00<\/td><td>\u975e\u8d2a\u5a6a<\/td><\/tr><tr><td><code>${var%%pattern}<\/code><\/td><td>\u5220\u9664<strong>\u6700\u957f\u5339\u914d<\/strong>\u7684\u540e\u7f00<\/td><td>\u8d2a\u5a6a<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>2.1\u3001\u83b7\u53d6\u6587\u4ef6\u6269\u5c55\u540d<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>filename=\"document.pdf\"\n\next=${filename##*.}      <em># =&gt; pdf \uff08\u5220\u9664\u6700\u957f\u5339\u914d\u7684 *. \u524d\u7f00\uff09<\/em>\n<em># \u6216\u7528\u622a\u53d6\uff08\u9700\u77e5\u9053\u957f\u5ea6\uff09<\/em>\next=${filename: -3}      <em># =&gt; pdf \uff08\u5047\u8bbe\u6269\u5c55\u540d3\u4f4d\uff09<\/em><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"has-medium-font-size\">\ud83d\udca1 \u66f4\u901a\u7528\u7684\u65b9\u5f0f\u662f\u7528 <code>${var##*.}<\/code>\uff0c\u4f46\u622a\u53d6\u9002\u7528\u4e8e\u56fa\u5b9a\u957f\u5ea6\u573a\u666f\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading has-large-font-size\"><strong>2.2\u3001\u53bb\u6389\u8def\u5f84\uff0c\u53ea\u7559\u6587\u4ef6\u540d<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>path=\"\/home\/user\/file.txt\"\n\nbasename=${path##*\/}     <em># =&gt; file.txt<\/em>\n<em># \u82e5\u5df2\u77e5\u8def\u5f84\u6df1\u5ea6\uff0c\u4e5f\u53ef\u7528\u622a\u53d6\uff08\u4e0d\u63a8\u8350\uff09<\/em><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Bash\uff08\u53ca\u5176\u4ed6 POSIX \u517c\u5bb9 shell\uff09\u4e2d\uff0c${} \u4e0d\u4ec5\u7528\u4e8e\u53d8\u91cf\u5f15\u7528\uff0c\u8fd8\u652f\u6301\u4e30\u5bcc\u7684**\u53c2\u6570\u6269\u5c55\uff08P [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":450,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-my","format":"standard","meta":{"footnotes":""},"categories":[135,131,157],"tags":[400,237,687,401],"class_list":["post-2427","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-131","category-shell","tag-bash","tag-linux","tag-os","tag-shell"],"_links":{"self":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2427"}],"version-history":[{"count":1,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2427\/revisions"}],"predecessor-version":[{"id":2428,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/posts\/2427\/revisions\/2428"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=\/wp\/v2\/media\/450"}],"wp:attachment":[{"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.madbull.site\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}