{"id":170113,"date":"2020-01-23T10:00:02","date_gmt":"2020-01-23T02:00:02","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=170113"},"modified":"2020-01-15T13:56:17","modified_gmt":"2020-01-15T05:56:17","slug":"shell-bash-var","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/shell-bash-var.html","title":{"rendered":"shell\u7f16\u7a0b–bash\u53d8\u91cf\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n shell\u7f16\u7a0b--bash\u53d8\u91cf\u4ecb\u7ecd \uff0c\u53d8\u91cf\u662f\u8ba1\u7b97\u673a\u5185\u5b58\u7684\u5355\u5143\uff0c\u5176\u4e2d\u5b58\u653e\u7684\u503c\u53ef\u4ee5\u6539\u53d8\u3002\u5f53\u00a0Shell \u811a\u672c\u9700\u8981\u4fdd\u5b58\u4e00\u4e9b\u4fe1\u606f\u65f6\uff0c\u5982\u4e00\u4e2a\u6587\u4ef6\u540d\u6216\u4e00\u4e2a\u6570\u5b57\uff0c\u5c31\u628a\u5b83\u5b58\u653e\u5728\u4e00\u4e2a\u53d8\u91cf\u4e2d\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
bash\u53d8\u91cf\u7c7b\u578b <\/strong><\/span><\/div>\n
    \n
  • \u672c\u5730\u53d8\u91cf<\/li>\n<\/ul>\n
    \u672c\u5730\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e3a\u6574\u4e2abash\u8fdb\u7a0b\u3002\u5c40\u90e8\u53d8\u91cf\u5c5e\u4e8e\u672c\u5730\u53d8\u91cf\u3002\u5728bash\u4e2d\u58f0\u660e\u53d8\u91cf\u7684\u5468\u671f\u968f\u7740\u8be5\u8fdb\u7a0b\u7684\u5468\u671f\u800c\u5b58\u5728\u3002\r\n\u5b9a\u4e49\u672c\u5730\u53d8\u91cf\uff1aset<\/span> VARNAME=Jerry\r\n\u5f15\u7528\u53d8\u91cf\uff1a${VARNAME}<\/span>\uff0c\u62ec\u53f7\u53ef\u7701\u7565\r\n\u5c40\u90e8\u53d8\u91cf\u7684\u58f0\u660e\uff1alocal<\/span> VARNAME=VALUE<\/code><\/pre>\n
      \n
    • \u73af\u5883\u53d8\u91cf<\/li>\n<\/ul>\n
      \u73af\u5883\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e3a\u5f53\u524dshell\u8fdb\u7a0b\u53ca\u5176\u5b50\u8fdb\u7a0b\r\n<\/span>\u58f0\u660e\uff1aexport VARNAME=VALUE\u6216\u8005VARNAME=VALUE; export VARNAME<\/span>\r\n\r\n\u811a\u672c\u5728\u6267\u884c\u65f6\u4f1a\u542f\u52a8\u4e00\u4e2a\u5b50shell\u8fdb\u7a0b\r\n<\/span>\u547d\u4ee4\u884c\u4e2d\u542f\u52a8\u7684\u811a\u672c\u4f1a\u7ee7\u627f\u5f53\u524dshell\u7684\u73af\u5883\u53d8\u91cf\r\n<\/span>\u7cfb\u7edf\u81ea\u52a8\u6267\u884c\u7684\u811a\u672c(\u975e\u547d\u4ee4\u884c\u542f\u52a8)\u5c31\u9700\u8981\u81ea\u6211\u5b9a\u4e49\u9700\u8981\u5404\u73af\u5883\u53d8\u91cf<\/code><\/pre>\n
        \n
      • \u4f4d\u7f6e\u53d8\u91cf<\/li>\n<\/ul>\n
        $1<\/span>, $2<\/span>, ...\r\nshift<\/span>: \u5bf9\u53c2\u6570\u8fdb\u884c\u8f6e\u6362\uff0c\u9ed8\u8ba4\u8f6e\u6362\u4e00\u4e2a\u53c2\u6570\r\n\r\n#!\/bin\/bash<\/span>\r\n#<\/span>\r\necho<\/span> $1<\/span>\r\nshift<\/span>\r\necho<\/span> $1<\/span>\r\nshift<\/span>\r\necho<\/span> $1<\/span>\r\nshift<\/span>\r\n\r\n.\/filetest.sh \/etc\/fstab \/etc\/inittab\r\n    $1<\/span>\uff1a\/etc\/fstab \r\n    $2<\/span>\uff1a\/etc\/inittab <\/code><\/pre>\n
          \n
        • \u7279\u6b8a\u53d8\u91cf<\/li>\n<\/ul>\n
          $?<\/span>: \u4e0a\u4e00\u4e2a\u547d\u4ee4\u6267\u884c\u72b6\u6001\u7684\u8fd4\u56de\u503c\r\n    \u7a0b\u5e8f\u6267\u884c\uff0c\u53ef\u80fd\u6709\u4e24\u7c7b\u8fd4\u56de\u503c\uff1a\r\n        \u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\r\n        \u7a0b\u5e8f\u72b6\u6001\u8fd4\u56de\u4ee3\u7801(0<\/span>-255<\/span>)\r\n            0<\/span>: \u6b63\u786e\u6267\u884c\r\n            1<\/span>-255<\/span>: \u9519\u8bef\u6267\u884c\u30021<\/span>\u30012<\/span>\u3001127<\/span>\u7cfb\u7edf\u9884\u7559\r\n\r\n$#<\/span>: \u53c2\u6570\u7684\u4e2a\u6570\r\n$*<\/span>: \u53c2\u6570\u5217\u8868\r\n$@<\/span>: \u53c2\u6570\u5217\u8868<\/code><\/pre>\n
          \u53d8\u91cf\u7684\u64a4\u9500\u53ca\u67e5\u770b <\/strong><\/span><\/div>\n
            \n
          • \u64a4\u9500\u53d8\u91cf<\/li>\n<\/ul>\n
            unset<\/span>\u00a0VARNAME<\/code><\/pre>\n
              \n
            • \u67e5\u770b\u53d8\u91cf<\/li>\n<\/ul>\n
              se<\/span>t:<\/span> \u67e5\u770b\u5f53\u524dshell<\/span>\u4e2d\u7684\u53d8\u91cf(\u5305\u62ec\u73af\u5883\u53d8\u91cf\u548c\u672c\u5730\u53d8\u91cf)\r\n\r\nprintenv\u3001env\u3001export:<\/span> \u67e5\u770b\u5f53\u524dshell<\/span>\u4e2d\u7684\u73af\u5883\u53d8\u91cf<\/code><\/pre>\n
              \u53d8\u91cf\u547d\u540d\u89c4\u5219 <\/strong><\/span><\/div>\n
              1\u3001\u53d8\u91cf\u4e0e\u53d8\u91cf\u5185\u5bb9\u4ee5\u4e00\u4e2a\u7b49\u53f7\"=\"<\/span>\u6765\u8fde\u63a5\uff0c\u5982\u4e0b\u6240\u793a\uff1a\r\n    MyName=Dabric\r\n2\u3001\u7b49\u53f7\u4e24\u8fb9\u4e0d\u80fd\u76f4\u63a5\u63a5\u7a7a\u683c\u7b26\uff0c\u5982\u4e0b\u9519\u8bef\uff1a\r\n    MyName= Dabric \u6216 Myname=Dab ric\r\n3\u3001\u53d8\u91cf\u540d\u79f0\u53ea\u80fd\u662f\u82f1\u6587\u5b57\u6bcd\u4e0e\u6570\u5b57\uff0c\u4f46\u662f\u5f00\u5934\u5b57\u7b26\u4e0d\u80fd\u65f6\u6570\u5b57\uff1b\r\n4\u3001\u53d8\u91cf\u5185\u5bb9\u82e5\u6709\u7a7a\u683c\u7b26\u53ef\u4f7f\u7528\u53cc\u5f15\u53f7\u6216\u5355\u5f15\u53f7\u5c06\u53d8\u91cf\u5185\u5bb9\u7ed3\u5408\u8d77\u6765\uff0c\u4f46\u53cc\u5f15\u53f7\u5185\u7684\u7279\u6b8a\u5b57\u7b26\u5982$\u7b49\uff0c\u53ef\u4ee5\u4fdd\u6709\u539f\u672c\u7684\u7279\u6027\uff0c\u5982\u4e0b\u6240\u793a\uff1a\r\n    var=\"lang is $LANG<\/span>\"<\/span> \u5219echo<\/span> $var<\/span> \u53ef\u5f97 lang is en_US.UTF-8\r\n\u5355\u5f15\u53f7\u5185\u7684\u7279\u6b8a\u5b57\u7b26\u5219\u4ec5\u4e3a\u4e00\u822c\u5b57\u7b26\uff08\u7eaf\u6587\u672c\uff09\uff0c\u5982\u4e0b\u6240\u793a\uff1a\r\n    var='lang is $LANG'<\/span> \u5219echo<\/span> $var<\/span> \u53ef\u5f97 lang is $LANG<\/span>\r\n    \u603b\u4e4b\u5728bash\u4e2d\uff1a\r\n    \"\"<\/span>:\u5f31\u5f15\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u53d8\u91cf\u66ff\u6362\r\n    ''<\/span>:\u5f3a\u5f15\u7528\uff0c\u4e0d\u5b8c\u6210\u53d8\u91cf\u66ff\u6362\r\n5\u3001\u53ef\u4ee5\u4f7f\u7528\u8df3\u8131\u5b57\u7b26\"\\\"\u5c06\u7279\u6b8a\u5b57\u7b26\uff08\u5982[ENTER], $, \\, \u7a7a\u683c\u7b26, '\u7b49\uff09\u53d8\u6210\u4e00\u822c\u5b57\u7b26\uff0c\u5982\uff1a\r\n    MyName=Dab\\ ric\r\n6\u3001\u5728\u4e00\u4e32\u6307\u4ee4\u7684\u6267\u884c\u4e2d\uff0c\u8fd8\u9700\u8981\u85c9\u7531\u5176\u4ed6\u989d\u5916\u7684\u6307\u4ee4\u6240\u63d0\u4f9b\u7684\u4fe1\u606f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cd\u5f15\u53f7\"<\/span>`COMMAND`\"\u6216\"<\/span>$(COMMAND)\"\u3002\u4f8b\u5982\u60f3\u8981\u53d6\u5f97\u6838\u5fc3\u7248\u672c\u7684\u8bbe\u5b9a\uff1a\r\n    version=$(uname -r)<\/span> \r\n    echo $version<\/span>\r\n\u5982\u4f55\u8fdb\u5165\u5230\u60a8\u76ee\u524d\u6838\u5fc3\u7684\u6a21\u5757\u76ee\u5f55\uff1a\r\n    # cd \/lib\/modules\/`uname -r`\/kernel\r\n    # cd \/lib\/moudles\/$(uname -r)<\/span>\/kernel\r\n7\u3001\u82e5\u6539\u53d8\u91cf\u4e3a\u6269\u589e\u53d8\u91cf\u5185\u5bb9\u65f6\uff0c\u5219\u53ef\u7528\"<\/span>$\u53d8\u91cf\u540d\u79f0\"\u6216${\u53d8\u91cf}<\/span>\u7d2f\u52a0\u5185\u5bb9\uff0c\u5982\u4e0b\u6240\u793a\uff1a\r\n    PATH=\"<\/span>$PATH<\/span>\":\/home\/bin \u6216 PATH=${PATH}<\/span>:\/home\/bin\r\n8\u3001\u82e5\u8be5\u53d8\u91cf\u9700\u8981\u5728\u5176\u4ed6\u5b50\u7a0b\u5e8f\u6267\u884c\uff0c\u5219\u9700\u8981\u4ee5export\u6765\u4f7f\u53d8\u91cf\u53d8\u6210\u73af\u5883\u53d8\u91cf\uff1a\r\n    # export PATH\r\n9\u3001\u901a\u5e38\u5927\u5199\u5b57\u7b26\u4e3a\u7cfb\u7edf\u9ed8\u8ba4\u53d8\u91cf\uff0c\u81ea\u884c\u8bbe\u5b9a\u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528\u5c0f\u5199\u5b57\u7b26\uff0c\u65b9\u4fbf\u5224\u65ad\uff1b\r\n10\u3001\u53d6\u6d88\u53d8\u91cf\u7684\u65b9\u6cd5\u4e3a\u4f7f\u7528unset\uff1a\"<\/span>unset<\/span> \u53d8\u91cf\u540d\u79f0\"\u4f8b\u5982\u53d6\u6d88MyName\u7684\u8bbe\u5b9a\uff1a\r\n    # unset MyName\r\n\u53d8\u91cf\u53ef\u4ee5\u5728bash\u914d\u7f6e\u6587\u4ef6(~\/.bashrc)\u4e2d\u76f4\u63a5\u6307\u5b9a\uff0c\u4fdd\u8bc1\u6bcf\u6b21\u767b\u5f55shell\u8be5\u53d8\u91cf\u90fd\u5b58\u5728<\/span><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

              \u672c\u5730\u53d8\u91cf \u672c\u5730\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u4e3a\u6574\u4e2abash\u8fdb\u7a0b\u3002\u5c40\u90e8\u53d8\u91cf\u5c5e\u4e8e\u672c\u5730\u53d8\u91cf\u3002\u5728bash\u4e2d\u58f0\u660e\u53d8\u91cf\u7684\u5468\u671f\u968f\u7740\u8be5\u8fdb\u7a0b\u7684\u5468\u671f […]<\/p>\n","protected":false},"author":643,"featured_media":170338,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-170113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/170113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=170113"}],"version-history":[{"count":9,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/170113\/revisions"}],"predecessor-version":[{"id":170404,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/170113\/revisions\/170404"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/170338"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=170113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=170113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=170113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}