{"id":1452,"date":"2023-03-23T22:23:18","date_gmt":"2023-03-23T14:23:18","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1452"},"modified":"2023-04-28T20:48:38","modified_gmt":"2023-04-28T12:48:38","slug":"linux-echo-display-content-color","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/23\/linux-echo-display-content-color\/","title":{"rendered":"Linux echo \u663e\u793a\u5185\u5bb9\u989c\u8272"},"content":{"rendered":"<h2>\u683c\u5f0f<\/h2>\n<p>echo \u663e\u793a\u5185\u5bb9\u989c\u8272\uff0c\u9700\u8981\u4f7f\u7528<code>-e<\/code>\u53c2\u6570<\/p>\n<ul>\n<li><code>-e<\/code>: \u6253\u5f00\u53cd\u659c\u6760\u8f6c\u4e49 (\u9ed8\u8ba4\u4e0d\u6253\u5f00)\uff0c\u53ef\u4ee5\u8f6c\u4e49<code>\\n,\\t<\/code>\u7b49<\/li>\n<li><code>-n<\/code>: \u5728\u6700\u540e\u4e0d\u81ea\u52a8\u6362\u884c<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<pre><code class=\"language-bash\">str=&quot;Joe.Ye&quot;\necho -e &quot;\\033[\u5b57\u80cc\u666f\u989c\u8272;\u6587\u5b57\u989c\u8272m ${str} \\033[0m&quot;<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\uff1a\u6587\u5b57\u989c\u8272\u540e\u9762\u6709\u4e2am <\/p>\n<\/blockquote>\n<h2>\u4e3e\u4f8b1\uff0c\u5b57\u4f53\u989c\u8272<\/h2>\n<pre><code class=\"language-bash\">#\u5b57\u4f53\u989c\u8272\uff1a30m-37m \u9ed1\u3001\u7ea2\u3001\u7eff\u3001\u9ec4\u3001\u84dd\u3001\u7d2b\u3001\u9752\u3001\u767d\nstr=&quot;kimbo zhang&quot;\necho -e &quot;\\033[30m ${str}\\033[0m&quot;      ## \u9ed1\u8272\u5b57\u4f53\necho -e &quot;\\033[31m ${str}\\033[0m&quot;      ## \u7ea2\u8272\necho -e &quot;\\033[32m ${str}\\033[0m&quot;      ## \u7eff\u8272\necho -e &quot;\\033[33m ${str}\\033[0m&quot;      ## \u9ec4\u8272\necho -e &quot;\\033[34m ${str}\\033[0m&quot;      ## \u84dd\u8272\necho -e &quot;\\033[35m ${str}\\033[0m&quot;      ## \u7d2b\u8272\necho -e &quot;\\033[36m ${str}\\033[0m&quot;      ## \u9752\u8272\necho -e &quot;\\033[37m ${str}\\033[0m&quot;      ## \u767d\u8272<\/code><\/pre>\n<h2>\u4e3e\u4f8b2\uff0c\u80cc\u666f\u8272\uff0b\u5b57\u4f53\u989c\u8272<\/h2>\n<pre><code class=\"language-bash\">#\u80cc\u666f\u989c\u8272\uff1a40-47 \u9ed1\u3001\u7ea2\u3001\u7eff\u3001\u9ec4\u3001\u84dd\u3001\u7d2b\u3001\u9752\u3001\u767d\nstr=&quot;kimbo zhang&quot;\necho -e &quot;\\033[41;37m ${str} \\033[0m&quot;     ## \u7ea2\u8272\u80cc\u666f\u8272\uff0c\u767d\u8272\u5b57\u4f53\necho -e &quot;\\033[41;33m ${str} \\033[0m&quot;     ## \u7ea2\u5e95\u9ec4\u5b57\necho -e &quot;\\033[1;41;33m ${str} \\033[0m&quot;   ## \u7ea2\u5e95\u9ec4\u5b57 \u9ad8\u4eae\u52a0\u7c97\u663e\u793a\necho -e &quot;\\033[5;41;33m ${str} \\033[0m&quot;   ## \u7ea2\u5e95\u9ec4\u5b57 \u5b57\u4f53\u95ea\u70c1\u663e\u793a\necho -e &quot;\\033[47;30m ${str} \\033[0m&quot;     ## \u767d\u5e95\u9ed1\u5b57\necho -e &quot;\\033[40;37m ${str} \\033[0m&quot;     ## \u9ed1\u5e95\u767d\u5b57<\/code><\/pre>\n<p>\u5176\u4ed6\u53c2\u6570\u8bf4\u660e<\/p>\n<pre><code class=\"language-bash\">\\033[1;m \u8bbe\u7f6e\u9ad8\u4eae\u52a0\u7c97\n\\033[4;m \u4e0b\u5212\u7ebf\n\\033[5;m \u95ea\u70c1<\/code><\/pre>\n<h2>\u5b9a\u4e49\u51fd\u6570\uff0c\u7528\u4e8e\u65e5\u5fd7\u5199\u5165\u7b49<\/h2>\n<pre><code class=\"language-bash\">#!\/bin\/bash\n## \u5199\u65e5\u5fd7\n## \u53c2\u65701\uff1a\u5b57\u7b26\u4e32\n## \u53c2\u65702\uff1a\u989c\u8272 (\u7ea2\u8272\uff1a\u5931\u8d25\u62a5\u9519\uff0c\u7eff\u8272\uff1a\u6210\u529f\uff0c\u9ec4\u8272\uff1a\u8b66\u544a)\n\nfunction func_write_log()\n{\n    var_str=$1\n    var_color=$2\n    var_curr_timestamp=`date &quot;+%Y-%m-%d %H:%M:%S&quot;`\n\n    ## \u5224\u65ad\u53c2\u65701 \u662f\u5426\u662f\u7a7a\u5b57\u7b26\u4e32\n    if [ &quot;x${var_str}&quot; == &quot;x&quot; ];then\n        var_str=&quot;&quot;\n    else\n        var_str=&quot;${var_curr_timestamp} ${var_str}&quot;\n    fi\n\n    ## \u5224\u65ad\u989c\u8272\n    if [ &quot;${var_color}&quot; == &quot;green&quot; ];then\n        var_str=&quot;\\n\\033[32m${var_str}\\033[0m&quot;\n    elif [ &quot;${var_color}&quot; == &quot;yellow&quot; ];then\n        var_str=&quot;\\033[33m${var_str}\\033[0m&quot;\n    elif [ &quot;${var_color}&quot; == &quot;red&quot; ];then\n        var_str=&quot;\\033[1;41;33m${var_str}\\033[0m&quot;\n    else\n        var_str=&quot;\\033[37m${var_str}\\033[0m&quot;\n    fi\n\n    ## \u6253\u5370\u8f93\u51fa\n    echo -e &quot;${var_str}&quot;\n    #echo -e &quot;${var_str}&quot; &gt;&gt; ${var_path}\/test_${var_curr_timestamp}.log 2&gt;&amp;1  #\u5199\u5165\u65e5\u5fd7\u6587\u4ef6\n}\n\n## \u51fd\u6570\u8c03\u7528\nfunc_write_log &quot;Joe&quot; &quot;red&quot;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u683c\u5f0f echo \u663e\u793a\u5185\u5bb9\u989c\u8272\uff0c\u9700\u8981\u4f7f\u7528-e\u53c2\u6570 -e: \u6253\u5f00\u53cd\u659c\u6760\u8f6c\u4e49 (\u9ed8\u8ba4\u4e0d\u6253\u5f00)\uff0c\u53ef\u4ee5\u8f6c\u4e49\\n,\\t\u7b49  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[331],"tags":[],"class_list":["post-1452","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1452"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1452\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}