{"id":1215,"date":"2019-04-29T09:53:45","date_gmt":"2019-04-29T01:53:45","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=1215"},"modified":"2019-04-29T09:53:45","modified_gmt":"2019-04-29T01:53:45","slug":"linux-bash-shell%e7%89%b9%e6%ae%8a%e5%8f%98%e9%87%8f%ef%bc%9ashell-0-%e5%92%8c%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%8f%82%e6%95%b0","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=1215","title":{"rendered":"linux bash Shell\u7279\u6b8a\u53d8\u91cf\uff1aShell $0, $#, $*, $@, $?, $$\u548c\u547d\u4ee4\u884c\u53c2\u6570"},"content":{"rendered":"<p>\u5728linux\u4e0b\u914d\u7f6eshell\u53c2\u6570\u8bf4\u660e<\/p>\n<p>\u524d\u9762\u5df2\u7ecf\u8bb2\u5230\uff0c\u53d8\u91cf\u540d\u53ea\u80fd\u5305\u542b\u6570\u5b57\u3001\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\uff0c\u56e0\u4e3a\u67d0\u4e9b\u5305\u542b\u5176\u4ed6\u5b57\u7b26\u7684\u53d8\u91cf\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u8fd9\u6837\u7684\u53d8\u91cf\u88ab\u79f0\u4e3a\u7279\u6b8a\u53d8\u91cf\u3002<br \/>\n\u4f8b\u5982\uff0c$ \u8868\u793a\u5f53\u524dShell\u8fdb\u7a0b\u7684ID\uff0c\u5373pid\uff0c\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<ol>\n<li>$echo $$<\/li>\n<\/ol>\n<p>\u8fd0\u884c\u7ed3\u679c<\/p>\n<pre>29949<\/pre>\n<p>\u7279\u6b8a\u53d8\u91cf\u5217\u8868<\/p>\n<p>\u53d8\u91cf<br \/>\n\u542b\u4e49<\/p>\n<p>$0<br \/>\n\u5f53\u524d\u811a\u672c\u7684\u6587\u4ef6\u540d<\/p>\n<p>$n<br \/>\n\u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u53c2\u6570\u3002n \u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u7b2c\u51e0\u4e2a\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f$1\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f$2\u3002<\/p>\n<p>$#<br \/>\n\u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570\u3002<\/p>\n<p>$*<br \/>\n\u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u6240\u6709\u53c2\u6570\u3002<\/p>\n<p>$@<br \/>\n\u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u6240\u6709\u53c2\u6570\u3002\u88ab\u53cc\u5f15\u53f7(&#8221; &#8220;)\u5305\u542b\u65f6\uff0c\u4e0e $* \u7a0d\u6709\u4e0d\u540c\uff0c\u4e0b\u9762\u5c06\u4f1a\u8bb2\u5230\u3002<\/p>\n<p>$?<br \/>\n\u4e0a\u4e2a\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\uff0c\u6216\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p>$$<br \/>\n\u5f53\u524dShell\u8fdb\u7a0bID\u3002\u5bf9\u4e8e Shell \u811a\u672c\uff0c\u5c31\u662f\u8fd9\u4e9b\u811a\u672c\u6240\u5728\u7684\u8fdb\u7a0bID\u3002<\/p>\n<h4>\u547d\u4ee4\u884c\u53c2\u6570<\/h4>\n<p>\u8fd0\u884c\u811a\u672c\u65f6\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53c2\u6570\u79f0\u4e3a\u547d\u4ee4\u884c\u53c2\u6570\u3002\u547d\u4ee4\u884c\u53c2\u6570\u7528 $n \u8868\u793a\uff0c\u4f8b\u5982\uff0c$1 \u8868\u793a\u7b2c\u4e00\u4e2a\u53c2\u6570\uff0c$2 \u8868\u793a\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002<br \/>\n\u8bf7\u770b\u4e0b\u9762\u7684\u811a\u672c\uff1a<\/p>\n<ol>\n<li>#!\/bin\/bash<\/li>\n<li><\/li>\n<li>echo &#8220;File Name: $0&#8221;<\/li>\n<li>echo &#8220;First Parameter : $1&#8221;<\/li>\n<li>echo &#8220;First Parameter : $2&#8221;<\/li>\n<li>echo &#8220;Quoted Values: $@&#8221;<\/li>\n<li>echo &#8220;Quoted Values: $*&#8221;<\/li>\n<li>echo &#8220;Total Number of Parameters : $#&#8221;<\/li>\n<\/ol>\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<pre>$.\/test.sh Zara Ali\nFile Name : .\/test.sh\nFirst Parameter : Zara\nSecond Parameter : Ali\nQuoted Values: Zara Ali\nQuoted Values: Zara Ali\nTotal Number of Parameters : 2<\/pre>\n<h4>$* \u548c $@ \u7684\u533a\u522b<\/h4>\n<p>$* \u548c $@ \u90fd\u8868\u793a\u4f20\u9012\u7ed9\u51fd\u6570\u6216\u811a\u672c\u7684\u6240\u6709\u53c2\u6570\uff0c\u4e0d\u88ab\u53cc\u5f15\u53f7(&#8221; &#8220;)\u5305\u542b\u65f6\uff0c\u90fd\u4ee5&#8221;$1&#8221; &#8220;$2&#8221; \u2026 &#8220;$n&#8221; \u7684\u5f62\u5f0f\u8f93\u51fa\u6240\u6709\u53c2\u6570\u3002<br \/>\n\u4f46\u662f\u5f53\u5b83\u4eec\u88ab\u53cc\u5f15\u53f7(&#8221; &#8220;)\u5305\u542b\u65f6\uff0c&#8221;$*&#8221; \u4f1a\u5c06\u6240\u6709\u7684\u53c2\u6570\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\uff0c\u4ee5&#8221;$1 $2 \u2026 $n&#8221;\u7684\u5f62\u5f0f\u8f93\u51fa\u6240\u6709\u53c2\u6570\uff1b&#8221;$@&#8221; \u4f1a\u5c06\u5404\u4e2a\u53c2\u6570\u5206\u5f00\uff0c\u4ee5&#8221;$1&#8243; &#8220;$2&#8221; \u2026 &#8220;$n&#8221; \u7684\u5f62\u5f0f\u8f93\u51fa\u6240\u6709\u53c2\u6570\u3002<br \/>\n\u4e0b\u9762\u7684\u4f8b\u5b50\u53ef\u4ee5\u6e05\u695a\u7684\u770b\u5230 $* \u548c $@ \u7684\u533a\u522b\uff1a<\/p>\n<ol>\n<li>#!\/bin\/bash<\/li>\n<li>echo &#8220;\\$*=&#8221; $*<\/li>\n<li>echo &#8220;\\&#8221;\\$*\\&#8221;=&#8221; &#8220;$*&#8221;<\/li>\n<li><\/li>\n<li>echo &#8220;\\$@=&#8221; $@<\/li>\n<li>echo &#8220;\\&#8221;\\$@\\&#8221;=&#8221; &#8220;$@&#8221;<\/li>\n<li><\/li>\n<li>echo &#8220;print each param from \\$*&#8221;<\/li>\n<li>for var in $*<\/li>\n<li>do<\/li>\n<li>echo &#8220;$var&#8221;<\/li>\n<li>done<\/li>\n<li><\/li>\n<li>echo &#8220;print each param from \\$@&#8221;<\/li>\n<li>for var in $@<\/li>\n<li>do<\/li>\n<li>echo &#8220;$var&#8221;<\/li>\n<li>done<\/li>\n<li><\/li>\n<li>echo &#8220;print each param from \\&#8221;\\$*\\&#8221;&#8221;<\/li>\n<li>for var in &#8220;$*&#8221;<\/li>\n<li>do<\/li>\n<li>echo &#8220;$var&#8221;<\/li>\n<li>done<\/li>\n<li><\/li>\n<li>echo &#8220;print each param from \\&#8221;\\$@\\&#8221;&#8221;<\/li>\n<li>for var in &#8220;$@&#8221;<\/li>\n<li>do<\/li>\n<li>echo &#8220;$var&#8221;<\/li>\n<li>done<\/li>\n<\/ol>\n<p>\u6267\u884c .\/test.sh &#8220;a&#8221; &#8220;b&#8221; &#8220;c&#8221; &#8220;d&#8221;\uff0c\u770b\u5230\u4e0b\u9762\u7684\u7ed3\u679c\uff1a<\/p>\n<pre>$*=  a b c d\n\"$*\"= a b c d\n$@=  a b c d\n\"$@\"= a b c d\nprint each param from $*\na\nb\nc\nd\nprint each param from $@\na\nb\nc\nd\nprint each param from \"$*\"\na b c d\nprint each param from \"$@\"\na\nb\nc\nd<\/pre>\n<h4>\u9000\u51fa\u72b6\u6001<\/h4>\n<p>$? \u53ef\u4ee5\u83b7\u53d6\u4e0a\u4e00\u4e2a\u547d\u4ee4\u7684\u9000\u51fa\u72b6\u6001\u3002\u6240\u8c13\u9000\u51fa\u72b6\u6001\uff0c\u5c31\u662f\u4e0a\u4e00\u4e2a\u547d\u4ee4\u6267\u884c\u540e\u7684\u8fd4\u56de\u7ed3\u679c\u3002<br \/>\n\u9000\u51fa\u72b6\u6001\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5927\u90e8\u5206\u547d\u4ee4\u6267\u884c\u6210\u529f\u4f1a\u8fd4\u56de 0\uff0c\u5931\u8d25\u8fd4\u56de 1\u3002<br \/>\n\u4e0d\u8fc7\uff0c\u4e5f\u6709\u4e00\u4e9b\u547d\u4ee4\u8fd4\u56de\u5176\u4ed6\u503c\uff0c\u8868\u793a\u4e0d\u540c\u7c7b\u578b\u7684\u9519\u8bef\u3002<br \/>\n\u4e0b\u9762\u4f8b\u5b50\u4e2d\uff0c\u547d\u4ee4\u6210\u529f\u6267\u884c\uff1a<\/p>\n<pre>$.\/test.sh Zara Ali\nFile Name : .\/test.sh\nFirst Parameter : Zara\nSecond Parameter : Ali\nQuoted Values: Zara Ali\nQuoted Values: Zara Ali\nTotal Number of Parameters : 2\n$echo $?\n0\n$<\/pre>\n<p>$? \u4e5f\u53ef\u4ee5\u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u540e\u7eed\u5c06\u4f1a\u8bb2\u89e3<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728linux\u4e0b\u914d\u7f6eshell\u53c2\u6570\u8bf4\u660e \u524d\u9762\u5df2\u7ecf\u8bb2\u5230\uff0c\u53d8\u91cf\u540d\u53ea\u80fd\u5305\u542b\u6570\u5b57\u3001\u5b57\u6bcd\u548c\u4e0b\u5212\u7ebf\uff0c\u56e0\u4e3a\u67d0\u4e9b\u5305\u542b\u5176\u4ed6\u5b57\u7b26\u7684\u53d8\u91cf\u6709\u7279\u6b8a\u542b\u4e49\uff0c\u8fd9\u6837\u7684\u53d8\u91cf\u88ab\u79f0\u4e3a\u7279\u6b8a\u53d8\u91cf\u3002 \u4f8b\u5982\uff0c$ \u8868\u793a\u5f53\u524dShell\u8fdb\u7a0b\u7684ID\uff0c\u5373pid\uff0c\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a $echo $$ \u8fd0\u884c\u7ed3\u679c 29949 \u7279\u6b8a\u53d8\u91cf\u5217\u8868 \u53d8\u91cf \u542b\u4e49 $0 \u5f53\u524d\u811a\u672c\u7684\u6587\u4ef6\u540d $n \u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u53c2\u6570\u3002n \u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u8868\u793a\u7b2c\u51e0\u4e2a\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f$1\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f$2\u3002 $# \u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570\u3002 $* \u4f20\u9012\u7ed9\u811a\u672c\u6216\u51fd\u6570\u7684\u6240\u6709\u53c2\u6570\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1215","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1215"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/1215\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}