{"id":154289,"date":"2024-05-08T11:29:46","date_gmt":"2024-05-08T03:29:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/154289.html"},"modified":"2024-05-08T11:29:49","modified_gmt":"2024-05-08T03:29:49","slug":"%e5%9c%a8-shell-%e7%bc%96%e7%a8%8b%e5%bc%80%e5%8f%91%e4%b8%ad%ef%bc%8c%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e5%8f%98%e9%87%8f%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/154289.html","title":{"rendered":"\u5728 shell \u7f16\u7a0b\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u8f93\u51fa\u53d8\u91cf\u7684\u503c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26212545\/420b8cc2-af8a-4b4a-8939-dbbd067dbe73.webp\" alt=\"\u5728 shell \u7f16\u7a0b\u5f00\u53d1\u4e2d\uff0c\u5982\u4f55\u8f93\u51fa\u53d8\u91cf\u7684\u503c\" \/><\/p>\n<p><p>\u5728shell\u7f16\u7a0b\u5f00\u53d1\u4e2d\uff0c\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u7684\u6838\u5fc3\u65b9\u5f0f\u6d89\u53ca<strong>\u4f7f\u7528echo\u547d\u4ee4\u3001printf\u547d\u4ee4\u3001\u76f4\u63a5\u91cd\u5b9a\u5411<\/strong>\u3002\u5176\u4e2d\uff0c<strong>echo\u547d\u4ee4<\/strong>\u662f\u6700\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5b83\u5141\u8bb8\u5728shell\u4e2d\u5feb\u901f\u8f93\u51fa\u53d8\u91cf\u503c\u3002\u901a\u8fc7\u7b80\u5355\u5730\u5728\u53d8\u91cf\u540d\u524d\u52a0\u4e0a\u4e00\u4e2a\u7f8e\u5143\u7b26\u53f7($)\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9echo\u547d\u4ee4\uff0c\u5c31\u80fd\u5b9e\u73b0\u53d8\u91cf\u503c\u7684\u8f93\u51fa\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528ECHO\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>Echo\u547d\u4ee4\u662fUnix\/Linux\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4e00\u4e2a\u547d\u4ee4\uff0c\u7528\u4e8e\u5728\u7ec8\u7aef\u8f93\u51fa\u5b57\u7b26\u4e32\u6216\u53d8\u91cf\u7684\u503c\u3002\u4f7f\u7528echo\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u975e\u5e38\u7b80\u5355\uff0c\u4f60\u53ea\u9700\u8981\u5728\u53d8\u91cf\u540d\u524d\u52a0\u4e0a\u4e00\u4e2a\u7f8e\u5143\u7b26\u53f7($)\u3002\u4f8b\u5982\uff0c\u82e5\u6709\u4e00\u4e2a\u53d8\u91cf\u540d\u4e3a<code>VAR<\/code>\uff0c\u5176\u5b58\u50a8\u7684\u503c\u4e3a<code>Hello, world<\/code>\uff0c\u90a3\u4e48\u901a\u8fc7\u6267\u884c\u547d\u4ee4<code>echo $VAR<\/code>\u5c31\u53ef\u4ee5\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u51fa<code>Hello, world<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cecho\u547d\u4ee4\u9664\u4e86\u53ef\u4ee5\u8f93\u51fa\u5355\u4e2a\u53d8\u91cf\u5916\uff0c\u8fd8\u652f\u6301\u8f93\u51fa\u591a\u4e2a\u53d8\u91cf\u548c\u5b57\u7b26\u4e32\u7684\u7ec4\u5408\u3002\u8fd9\u5728\u811a\u672c\u7f16\u7a0b\u4e2d\u5341\u5206\u6709\u7528\uff0c\u6bd4\u5982\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u5e26\u6709\u52a8\u6001\u5185\u5bb9\u7684\u6587\u4ef6\u8def\u5f84\u6216\u6d88\u606f\u3002\u6b64\u5916\uff0cecho\u8fd8\u652f\u6301\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\uff0c\u5982\u6362\u884c\u7b26<code>\\n<\/code>\u3001\u5236\u8868\u7b26<code>\\t<\/code>\u7b49\uff0c\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u7528\u4e8e\u6539\u5584\u8f93\u51fa\u5185\u5bb9\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528PRINTF\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u4e0eecho\u547d\u4ee4\u76f8\u6bd4\uff0cprintf\u547d\u4ee4\u63d0\u4f9b\u4e86\u66f4\u4e3a\u590d\u6742\u548c\u5f3a\u5927\u7684\u683c\u5f0f\u63a7\u5236\u529f\u80fd\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5bf9\u8f93\u51fa\u683c\u5f0f\u8fdb\u884c\u7cbe\u786e\u63a7\u5236\uff0c\u5305\u62ec\u53d8\u91cf\u503c\u7684\u5bbd\u5ea6\u3001\u5bf9\u9f50\u65b9\u5f0f\u3001\u5c0f\u6570\u70b9\u540e\u4f4d\u6570\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528printf\u547d\u4ee4\u8f93\u51fa\u53d8\u91cf\u7684\u57fa\u672c\u8bed\u6cd5\u662f<code>printf format-string [arguments...]<\/code>\u3002\u5176\u4e2d\uff0c<code>format-string<\/code>\u662f\u683c\u5f0f\u63a7\u5236\u5b57\u7b26\u4e32\uff0c\u800c<code>arguments<\/code>\u5219\u662f\u8981\u8f93\u51fa\u7684\u53d8\u91cf\u6216\u503c\u3002\u6bd4\u5982\uff0c<code>printf &quot;%s\\n&quot; $VAR<\/code>\u4f1a\u4ee5\u9ed8\u8ba4\u683c\u5f0f\u8f93\u51fa<code>VAR<\/code>\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u5728\u5176\u540e\u6dfb\u52a0\u4e00\u4e2a\u6362\u884c\u7b26\u3002\u7531\u4e8eprintf\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u683c\u5f0f\u63a7\u5236\u9009\u9879\uff0c\u56e0\u6b64\u5b83\u7279\u522b\u9002\u5408\u8f93\u51fa\u9700\u8981\u7cbe\u786e\u683c\u5f0f\u63a7\u5236\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u91cd\u5b9a\u5411\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u5728shell\u811a\u672c\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u91cd\u5b9a\u5411\u64cd\u4f5c\u7b26\u5c06\u53d8\u91cf\u7684\u503c\u8f93\u51fa\u5230\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u5c06\u6807\u51c6\u8f93\u51fa\uff08STDOUT\uff09\u91cd\u5b9a\u5411\u5230\u4e00\u4e2a\u6587\u4ef6\uff0c\u4ece\u800c\u5b9e\u73b0\u5c06\u53d8\u91cf\u7684\u503c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u5730\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c<code>echo $VAR &gt; file.txt<\/code>\u547d\u4ee4\u4f1a\u5c06<code>VAR<\/code>\u53d8\u91cf\u7684\u503c\u5199\u5165<code>file.txt<\/code>\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5df2\u7ecf\u5b58\u5728\uff0c\u8be5\u547d\u4ee4\u4f1a\u8986\u76d6\u539f\u6709\u5185\u5bb9\uff1b\u5982\u679c\u9700\u8981\u8ffd\u52a0\u5185\u5bb9\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>&gt;&gt;<\/code>\u64cd\u4f5c\u7b26\uff0c\u5982<code>echo $VAR &gt;&gt; file.txt<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u4e8e\u65e5\u5fd7\u8bb0\u5f55\u3001\u6570\u636e\u5907\u4efd\u7b49\u573a\u666f\uff0c\u5728\u811a\u672c\u7f16\u7a0b\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u7efc\u5408\u5e94\u7528\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u7684shell\u7f16\u7a0b\u4e2d\uff0c\u901a\u5e38\u4f1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u6700\u5408\u9002\u7684\u53d8\u91cf\u8f93\u51fa\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5f53\u4ec5\u9700\u8981\u5feb\u901f\u67e5\u770b\u53d8\u91cf\u5185\u5bb9\u65f6\uff0cecho\u547d\u4ee4\u662f\u6700\u7b80\u5355\u7684\u9009\u62e9\uff1b\u82e5\u9700\u5bf9\u8f93\u51fa\u683c\u5f0f\u6709\u5177\u4f53\u8981\u6c42\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528printf\u547d\u4ee4\uff1b\u800c\u9700\u8981\u5c06\u53d8\u91cf\u7684\u503c\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u65f6\uff0c\u5219\u9009\u62e9\u91cd\u5b9a\u5411\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8003\u8651\u5230\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u63a8\u8350\u5728\u8f93\u51fa\u53d8\u91cf\u65f6\u59cb\u7ec8\u4f7f\u7528\u53cc\u5f15\u53f7\u5c06\u53d8\u91cf\u5f15\u8d77\u6765\uff0c\u5982<code>echo &quot;$VAR&quot;<\/code>\u3002\u8fd9\u53ef\u4ee5\u9632\u6b62\u53d8\u91cf\u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u6216\u7a7a\u767d\u5f71\u54cd\u547d\u4ee4\u7684\u6267\u884c\uff0c\u5e76\u4e14\u5728\u53d8\u91cf\u672a\u5b9a\u4e49\u65f6\u907f\u514d\u51fa\u73b0\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u901a\u8fc7\u638c\u63e1echo\u547d\u4ee4\u3001printf\u547d\u4ee4\u4ee5\u53ca\u91cd\u5b9a\u5411\u6280\u672f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5728shell\u811a\u672c\u4e2d\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u7406\u89e3\u5e76\u719f\u7ec3\u8fd0\u7528\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\uff0c\u5bf9\u63d0\u9ad8shell\u7f16\u7a0b\u7684\u6548\u7387\u548c\u8d28\u91cf\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u4f7f\u7528 echo \u547d\u4ee4\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff1f<\/strong><\/p>\n<p>\u5728 shell \u7f16\u7a0b\u4e2d\uff0c\u4f7f\u7528 echo \u547d\u4ee4\u53ef\u4ee5\u5c06\u5185\u5bb9\u8f93\u51fa\u5230\u6807\u51c6\u8f93\u51fa\u3002\u8981\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff0c\u53ea\u9700\u5728\u53d8\u91cf\u540d\u524d\u9762\u52a0\u4e0a $ \u7b26\u53f7\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a name \u7684\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8f93\u51fa\u5b83\u7684\u503c\uff1a<\/p>\n<pre><code>name=&quot;John&quot;\necho $name\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5728\u7ec8\u7aef\u4e0a\u663e\u793a\u53d8\u91cf name \u7684\u503c\uff0c\u5373 &quot;John&quot;\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u4f7f\u7528 printf \u547d\u4ee4\u683c\u5f0f\u5316\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u4f7f\u7528 echo \u547d\u4ee4\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 printf \u547d\u4ee4\u6765\u683c\u5f0f\u5316\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u3002\u4e0e echo \u547d\u4ee4\u4e0d\u540c\uff0cprintf \u547d\u4ee4\u5141\u8bb8\u4f60\u6307\u5b9a\u8f93\u51fa\u7684\u683c\u5f0f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a age \u7684\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8f93\u51fa\u5b83\u7684\u503c\uff0c\u5e76\u6307\u5b9a\u8f93\u51fa\u683c\u5f0f\u4e3a\u6574\u6570\uff1a<\/p>\n<pre><code>age=25\nprintf &quot;\u5e74\u9f84\uff1a%d\\n&quot; $age\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5728\u7ec8\u7aef\u4e0a\u663e\u793a &quot;\u5e74\u9f84\uff1a25&quot;\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528\u7279\u6b8a\u53d8\u91cf $ \u53ca {} \u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff1f<\/strong><\/p>\n<p>\u5728 shell \u7f16\u7a0b\u4e2d\uff0c\u7279\u6b8a\u53d8\u91cf $ \u53ca {} \u53ef\u4ee5\u7528\u4e8e\u8f93\u51fa\u53d8\u91cf\u7684\u503c\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u64cd\u4f5c\u53d8\u91cf\u3002<\/p>\n<p>\u4f7f\u7528 ${} \u5c06\u53d8\u91cf\u540d\u62ec\u8d77\u6765\u53ef\u4ee5\u5e2e\u52a9\u4f60\u660e\u786e\u6307\u5b9a\u53d8\u91cf\u7684\u8fb9\u754c\uff0c\u907f\u514d\u4ea7\u751f\u6b67\u4e49\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u540d\u4e3a count \u7684\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8f93\u51fa\u5b83\u7684\u503c\uff1a<\/p>\n<pre><code>count=10\necho &quot;Count: ${count}&quot;\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u5728\u7ec8\u7aef\u4e0a\u663e\u793a &quot;Count: 10&quot;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728shell\u7f16\u7a0b\u5f00\u53d1\u4e2d\uff0c\u8f93\u51fa\u53d8\u91cf\u7684\u503c\u7684\u6838\u5fc3\u65b9\u5f0f\u6d89\u53ca\u4f7f\u7528echo\u547d\u4ee4\u3001printf\u547d\u4ee4\u3001\u76f4\u63a5\u91cd\u5b9a\u5411\u3002\u5176\u4e2d\uff0cech [&hellip;]","protected":false},"author":3,"featured_media":154295,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/154289"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=154289"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/154289\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/154295"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=154289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=154289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=154289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}