{"id":224719,"date":"2021-08-26T08:33:19","date_gmt":"2021-08-26T00:33:19","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=224719"},"modified":"2021-08-20T14:34:25","modified_gmt":"2021-08-20T06:34:25","slug":"bash-brace-exp","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/bash-brace-exp.html","title":{"rendered":"\u5982\u4f55\u5728bash\u4e2d\u4f7f\u7528{}\u8303\u56f4\u8868\u8fbe\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728\u7f16\u5199 shell \u811a\u672c\u65f6\uff0c\u6709\u65f6\u9700\u8981\u751f\u6210\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u5e8f\u5217\u3002\u8fd9\u79cd\u5e8f\u5217\u6570\u636e\u7684\u4e00\u79cd\u5e38\u89c1\u7528\u9014\u662f\u7528\u4e8e\u5faa\u73af\u8fed\u4ee3\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u867d\u7136\u53ef\u4ee5\u4f7f\u7528 seq<\/code> \u4e4b\u7c7b\u7684\u4e13\u7528\u5de5\u5177\u6765\u751f\u6210\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4f46 bash \u672c\u8eab\u63d0\u4f9b\u5927\u62ec\u53f7\u6269\u5c55\uff0c\u5b9e\u9645\u4e0a\u6ca1\u6709\u5fc5\u8981\u5728 bash \u811a\u672c\u4e2d\u6dfb\u52a0\u6b64\u7c7b\u5916\u90e8\u4f9d\u8d56\u9879\u3002\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u8ba9\u6211\u4eec\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u5927\u62ec\u53f7\u6269\u5c55\u5728 shell \u811a\u672c\u4e2d\u751f\u6210\u6570\u636e\u5e8f\u5217\u548c\u4e00\u4e9b\u6709\u7528\u7684\u5927\u62ec\u53f7\u6269\u5c55\u793a\u4f8b\u3002<\/p>\n

{}\u82b1\u62ec\u53f7\u4f7f\u7528\u8bf4\u660e<\/strong><\/div>\n

Bash \u5185\u7f6e\u7684 range \u51fd\u6570\u662f\u901a\u8fc7\u6240\u8c13\u7684{}<\/code>\u5927\u62ec\u53f7\u6269\u5c55\u5b9e\u73b0\u7684\u3002\u7b80\u800c\u8a00\u4e4b\uff0c\u5927\u62ec\u53f7\u6269\u5c55\u5141\u8bb8\u6839\u636e\u63d0\u4f9b\u7684\u5b57\u7b26\u4e32\u548c\u6570\u5b57\u6570\u636e\u751f\u6210\u5b57\u7b26\u4e32\u5e8f\u5217\u3002\u5927\u62ec\u53f7\u6269\u5c55\u7684\u8bed\u6cd5\u5982\u4e0b\u3002<\/p>\n

\r\n{<string1>,<string2>,...,<stringN>}\r\n{<start-number>..<end-number>}\r\n{<start-number>..<end-number>..<increment>}\r\n<prefix-string>{......}\r\n{......}<suffix-string>\r\n<prefix-string>{......}<suffix-string>\r\n<\/pre>\n
\u5b9e\u4f8b\u4e00\uff1a\u5217\u51fa\u5b57\u7b26\u4e32\u5e8f\u5217<\/strong><\/div>\n

\u5927\u62ec\u53f7\u6269\u5c55\u7684\u7b2c\u4e00\u4e2a\u7528\u4f8b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b57\u7b26\u4e32\u5217\u8868\uff0c\u5b83\u662f\u5927\u62ec\u53f7\u5185\u4ee5\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u5217\u8868\u3002\u8fd9\u91cc\u662f\u7b80\u5355\u5730\u5217\u51fa\u9884\u5b9a\u4e49\u7684\u5b57\u7b26\u4e32\u6570\u636e\u3002<\/p>\n

\u4e0b\u9762\u4f7f\u7528for\u5faa\u73af\uff0c\u5217\u51fa\u5927\u62ec\u53f7\u4e2d\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u4e0b\u6240\u793a\u3002<\/p>\n

\r\n[root@localhost ~]# for fruit in {apple,orange,lemon}; do echo $fruit ; done\r\napple\r\norange\r\nlemon\r\n<\/pre>\n

\"\"
\n\u4e0b\u9762\u5b9e\u4f8b\u662f\u540c\u65f6\u521b\u5efa\u591a\u4e2a\u5b50\u76ee\u5f55\uff1a<\/p>\n

\r\n[root@localhost ~]# mkdir -p \/tmp\/users\/{dan,john,alex,michael,emma}\r\n[root@localhost ~]# ls -l \/tmp\/users\/\r\ntotal 0\r\ndrwxr-xr-x 2 root root 6 Aug  6 16:23 alex\r\ndrwxr-xr-x 2 root root 6 Aug  6 16:23 dan\r\ndrwxr-xr-x 2 root root 6 Aug  6 16:23 emma\r\ndrwxr-xr-x 2 root root 6 Aug  6 16:23 john\r\ndrwxr-xr-x 2 root root 6 Aug  6 16:23 michael\r\n<\/pre>\n

\"\"
\n\u4e0b\u9762\u662f\u521b\u5efa\u591a\u4e2a\u7a7a\u6587\u4ef6\uff1a<\/p>\n

\r\n[root@localhost ~]# touch \/tmp\/file{1,2,3,4}.log\r\n[root@localhost ~]# ll \/tmp\/\r\ntotal 0\r\n-rw-r--r-- 1 root root  0 Aug  6 16:24 file1.log\r\n-rw-r--r-- 1 root root  0 Aug  6 16:24 file2.log\r\n-rw-r--r-- 1 root root  0 Aug  6 16:24 file3.log\r\n-rw-r--r-- 1 root root  0 Aug  6 16:24 file4.log\r\n<\/pre>\n

\"\"<\/p>\n

\u5b9e\u4f8b\u4e8c\uff1a\u5b9a\u4e49\u4e00\u4e2a\u6570\u5b57\u8303\u56f4<\/strong><\/div>\n

\u5927\u62ec\u53f7\u6269\u5c55\u6700\u5e38\u89c1\u7684\u7528\u4f8b\u662f\u4e3a\u5faa\u73af\u8fed\u4ee3\u5b9a\u4e49\u4e00\u4e2a\u6570\u5b57\u8303\u56f4\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8868\u8fbe\u5f0f\uff0c\u5728\u5176\u4e2d\u6307\u5b9a\u8303\u56f4\u7684\u5f00\u59cb\/\u7ed3\u675f\uff0c\u4ee5\u53ca\u53ef\u9009\u7684\u589e\u91cf\u503c\u3002<\/p>\n

\r\n{<start-number>..<end-number>}\r\n{<start-number>..<end-number>..<increment>}\r\n<\/pre>\n

\u8981\u5b9a\u4e49 10 \u5230 20 \u4e4b\u95f4\u7684\u6574\u6570\u5e8f\u5217\uff1a<\/p>\n

\r\n[root@localhost ~]# echo {10..20}\r\n10 11 12 13 14 15 16 17 18 19 20\r\n<\/pre>\n

\"\"
\n\u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u4f7f\u7528\uff1a<\/p>\n

\r\n[root@localhost ~]# for num in {10..20}; do echo $num ;done\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n<\/pre>\n

\"\"
\n\u5982\u679c\u8981\u751f\u6210 0 \u5230 20 \u4e4b\u95f4\u589e\u91cf\u4e3a 2 \u7684\u6570\u5b57\u5e8f\u5217\uff1a<\/p>\n

\r\n[root@localhost ~]# echo {0..20..2}\r\n0 2 4 6 8 10 12 14 16 18 20\r\n<\/pre>\n

\"\"
\n\u4e5f\u53ef\u4ee5\u751f\u6210\u4e00\u7cfb\u5217\u9012\u51cf\u6570\u5b57\uff1a<\/p>\n

\r\n[root@localhost ~]# echo {20..10}\r\n20 19 18 17 16 15 14 13 12 11 10\r\n\r\n[root@localhost ~]# echo {20..10..-2}\r\n20 18 16 14 12 10\r\n<\/pre>\n

\"\"
\n\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u524d\u5bfc\u96f6\u586b\u5145\u6570\u5b57\uff0c\u4ee5\u9632\u9700\u8981\u4f7f\u7528\u76f8\u540c\u6570\u91cf\u7684\u6570\u5b57\u3002\u4f8b\u5982\uff1a<\/p>\n

\r\n[root@localhost ~]# for num in {00..20..2}; do echo $num ; done\r\n00\r\n02\r\n04\r\n06\r\n08\r\n10\r\n12\r\n14\r\n16\r\n18\r\n20\r\n<\/pre>\n

\"\"<\/p>\n

\u5b9e\u4f8b\u4e09\uff1a\u751f\u6210\u5b57\u6bcd\u5e8f\u5217<\/strong><\/div>\n

\u5927\u62ec\u53f7\u6269\u5c55\u4e0d\u4ec5\u53ef\u7528\u4e8e\u751f\u6210\u6570\u5b57\u5e8f\u5217\uff0c\u8fd8\u53ef\u7528\u4e8e\u751f\u6210\u5b57\u6bcd\u5e8f\u5217\uff1a<\/p>\n

\r\n[root@localhost ~]# cat brace.sh \r\n#!\/bin\/bash\r\nfor char1 in {A..B}; do\r\n    for char2 in {A..B}; do\r\n        echo \"${char1}${char2}\"\r\n    done\r\ndone\r\n<\/pre>\n

\"\"<\/p>\n

\u5b9e\u4f8b\u56db\uff1a\u751f\u6210\u5e26\u6709\u524d\u7f00\u3001\u540e\u7f00\u7684\u5b57\u7b26\u4e32\u5e8f\u5217<\/strong><\/div>\n

\u4f7f\u7528\u6b64\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u6309\u987a\u5e8f\u7f16\u53f7\u7684\u6587\u4ef6\u540d\u5217\u8868\uff1a<\/p>\n

\r\n[root@localhost ~]# for file in img_{00..05}.jpg; do echo $file ; done\r\nimg_00.jpg\r\nimg_01.jpg\r\nimg_02.jpg\r\nimg_03.jpg\r\nimg_04.jpg\r\nimg_05.jpg\r\n<\/pre>\n

\"\"<\/p>\n

\u5b9e\u4f8b\u4e94\uff1a\u591a\u4e2a{}\u82b1\u62ec\u53f7\u7ec4\u5408\u4f7f\u7528<\/strong><\/div>\n

\u6700\u540e\uff0c\u53ef\u4ee5\u7ec4\u5408\u591a\u4e2a\u5927\u62ec\u53f7\u6269\u5c55\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4f1a\u751f\u6210\u6240\u6709\u53ef\u80fd\u7ec4\u5408\u3002<\/p>\n

\r\nfor char1 in {A..Z}; do\r\n    for char2 in {A..Z}; do\r\n        echo \"${char1}${char2}\"\r\n    done\r\ndone\r\n<\/pre>\n

\u901a\u8fc7\u7ec4\u5408\u4e24\u4e2a\u5927\u62ec\u53f7\u6269\u5c55\uff0c\u4e0b\u9762\u7684\u5355\u4e2a\u5faa\u73af\u53ef\u4ee5\u4ea7\u751f\u4e0e\u4e0a\u9762\u76f8\u540c\u7684\u8f93\u51fa\u3002<\/p>\n

\r\nfor str in {A..Z}{A..Z}; do\r\n    echo $str\r\ndone\r\n<\/pre>\n
\u603b\u7ed3<\/strong><\/div>\n

{}<\/code>\u5141\u8bb8\u60a8\u5728\u5355\u4e2a\u547d\u4ee4\u884c\u4e2d\u8f7b\u677e\u751f\u6210\u4e00\u7cfb\u5217\u4efb\u610f\u5b57\u7b26\u4e32\u3002\u5927\u62ec\u53f7\u6269\u5c55\u4e0d\u4ec5\u5bf9 shell \u811a\u672c\u6709\u7528\uff0c\u800c\u4e14\u5728\u547d\u4ee4\u884c\u73af\u5883\u4e2d\u4e5f\u5f88\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u867d\u7136\u53ef\u4ee5\u4f7f\u7528 seq \u4e4b\u7c7b\u7684\u4e13\u7528\u5de5\u5177\u6765\u751f\u6210\u4e00\u7cfb\u5217\u6570\u5b57\uff0c\u4f46 bash \u672c\u8eab\u63d0\u4f9b\u5927\u62ec\u53f7\u6269\u5c55\uff0c\u5b9e\u9645\u4e0a\u6ca1\u6709\u5fc5\u8981\u5728 ba […]<\/p>\n","protected":false},"author":309,"featured_media":224720,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-224719","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\/224719","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\/309"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=224719"}],"version-history":[{"count":1,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224719\/revisions"}],"predecessor-version":[{"id":224731,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224719\/revisions\/224731"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/224720"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=224719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=224719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=224719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}