{"id":2357,"date":"2015-07-12T11:00:30","date_gmt":"2015-07-12T03:00:30","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2357"},"modified":"2015-07-12T11:00:30","modified_gmt":"2015-07-12T03:00:30","slug":"%e5%9c%a8shell%e8%84%9a%e6%9c%ac%e4%b8%ad%e4%bd%bf%e7%94%a8alias%e5%88%ab%e5%90%8d","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2357.html","title":{"rendered":"\u5728shell\u811a\u672c\u4e2d\u4f7f\u7528alias\u522b\u540d"},"content":{"rendered":"<h6>\u7f18\u7531\uff1a<\/h6>\n<p>\u7ecf\u5e38\u7528Linux\u8fdb\u884c\u64cd\u4f5c\u7684\u540c\u5b66\u4e00\u822c\u90fd\u4f1a\u6709\u5404\u79cd\u5404\u6837\u7684\u6280\u5de7\u6765\u63d0\u5347\u5de5\u4f5c\u6548\u7387\uff0c\u800c\u6dfb\u52a0\/\u6539\u5199shell\u4e2d\u7684alias\u65e0\u7591\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u4e00\u79cd\uff1a\u901a\u8fc7\u8bbe\u7f6e\u81ea\u5df1\u60ef\u7528\u7684alias\u53ef\u4ee5\u5feb\u901f\u7684\u5728Linux\u547d\u4ee4\u884c\u4e2d\u8fdb\u884c\u64cd\u4f5c\uff0c\u51cf\u5c11\u952e\u76d8\u6572\u51fb\u3001\u63d0\u5347\u64cd\u4f5c\u6548\u7387\u3002\u4e0d\u8fc7\uff0c\u5927\u90e8\u5206\u4eba\u4ec5\u9650\u4e8e\u5728\u547d\u4ee4\u884c\u4e0a\u4f7f\u7528alias\uff0c\u5f88\u5c11\u6709\u4eba\u4f1a\u5728\u81ea\u5df1\u5199\u7684shell\u811a\u672c\u4e2d\u4e5f\u4f7f\u7528alias\uff0c\u4e00\u65b9\u9762\u662f\u56e0\u4e3aalias\u65e0\u6cd5\u901a\u7528\uff1a\u4e0d\u540c\u7684\u4eba\u6709\u4e0d\u540c\u7684\u4e60\u60ef\uff0c\u5e76\u4e14\u5728\u5b9e\u9645\u7684\u751f\u4ea7\u73af\u5883\u4e2d\u60c5\u51b5\u5343\u5dee\u4e07\u522b\uff0calias\u65e0\u6cd5\u4fdd\u8bc1\u811a\u672c\u7684\u901a\u7528\u6027\u548c\u6709\u6548\u6027\uff1b\u53e6\u4e00\u65b9\u9762\u5c31\u662f\u2014\u2014\u6709\u4e9b\u4eba\u771f\u7684\u4e0d\u77e5\u9053\u600e\u4e48\u5728shell\u811a\u672c\u4e2d\u4f7f\u7528alias\u3002\u800c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u5c31\u662f\u9488\u5bf9\u4e0d\u77e5\u9053\u600e\u4e48\u5728shell\u811a\u672c\u4e2d\u4f7f\u7528alias\u529f\u80fd\u7684\u4eba\u6240\u5199\u7684\u3002<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<ul>\n<li>bash shell alias<\/li>\n<li>bash expand_aliases<\/li>\n<\/ul>\n<h6>\u53c2\u8003\u89e3\u7b54\uff1a<\/h6>\n<pre class=\"lang:default decode:true \">#\u5728\u4f7f\u7528alias\u4e4b\u524d\u6dfb\u52a0\u4e0b\u9762\u7684\u5185\u5bb9\nshopt -s expand_aliases<\/pre>\n<p>\u67e5\u770bBash\u7684manual\u4f60\u80fd\u627e\u5230\u4e0b\u9762\u7684\u5185\u5bb9\uff1a<\/p>\n<blockquote>\n<p style=\"padding-left: 30px;\">Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).<\/p>\n<\/blockquote>\n<p>\u7ffb\u8bd1\u5982\u4e0b\uff1a<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5728<span style=\"color: #0000ff;\">\u975e<\/span>\u4ea4\u4e92\u5f0fshell\u4e2d\u65e0\u6cd5\u4f7f\u7528alias\uff0c\u9664\u975e\u7528shopt\u547d\u4ee4\u663e\u793a\u8bbe\u5b9aexpand_aliases\u9009\u9879\u3002<\/strong><\/span><\/p>\n<blockquote>\n<p style=\"padding-left: 30px;\">expand_aliases<br \/>\nIf set, aliases are expanded as described below under Aliases, <a href=\"http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Aliases.html#Aliases\" target=\"_blank\">Aliases<\/a>. This option is enabled by default for interactive shells.<\/p>\n<\/blockquote>\n<p>\u56e0\u6b64\uff0c\u5c06\u201cshopt -s expand_aliases\u201d\u6dfb\u52a0\u5230\u4f60\u7684\u811a\u672c\u4e2d\u53bb\uff0c\u7136\u540e\u8bb0\u5f97source(\u91cd\u65b0\u52a0\u8f7d)\u4f60\u7684\u522b\u540d\u6587\u4ef6\/\u76f8\u5e94\u811a\u672c\uff1a<\/p>\n<pre class=\"lang:default decode:true \">shopt -s expand_aliases\nsource ~\/.bash_aliases<\/pre>\n<p>\u6211\u5728<a href=\"http:\/\/ixyzero.com\/blog\/archives\/2306.html\" target=\"_blank\">\u524d\u9762\u7684\u4e00\u7bc7\u6587\u7ae0(Linux\u4e2d.bashrc\u548c.bash_profile\u533a\u522b)<\/a>\u4e2d\u63d0\u5230\u8fc7\uff1a~\/.bashrc \u8fd9\u4e2a\u6587\u4ef6\u4e00\u822c\u662f\u7531<span style=\"color: #ff0000;\"><strong>\u4ea4\u4e92\u5f0f<\/strong>\u975e\u767b\u5f55shell<\/span>\u8c03\u7528\u7684\uff0c\u800c\u5927\u90e8\u5206\u4eba\u4e00\u822c\u90fd\u662f\u5c06alias\u8bbe\u7f6e\u5728~\/.bashrc\u4e2d\u7684\uff0c\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u65e0\u6cd5\u5728shell\u811a\u672c\u4e2d\u4f7f\u7528alias\u7684\uff1b<strong>\u5982\u679c\u4f60\u771f\u7684\u9700\u8981\u5728\u4ee5\u540e\u7684shell\u7f16\u7a0b\u4e2d\u9ed8\u8ba4\u5f00\u542falias\u529f\u80fd<\/strong>\uff08<span style=\"color: #ff0000;\"><strong>\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a<\/strong><\/span>\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6eBASH_ENV\u8fd9\u4e2a\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9a\u975e\u4ea4\u4e92\u5f0fshell\u9700\u8981\u8c03\u7528\u7684\u6587\u4ef6\u6765\u5b9e\u73b0\u8be5\u76ee\u7684\uff0c\u867d\u7136\u6211\u5e76\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\uff1a<\/p>\n<pre class=\"lang:default decode:true\">#1.\u7ed9 ~\/.bashrc \u6dfb\u52a0\u4e00\u884c\u5185\u5bb9\u7528\u4e8e\u8bbe\u7f6eBASH_ENV\u73af\u5883\u53d8\u91cf\nBASH_ENV='~\/.non_interactive_shell'\n\n#2.\u8bbe\u7f6e ~\/.non_interactive_shell \u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\nshopt -s expand_aliases<\/pre>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/tldp.org\/LDP\/abs\/html\/aliases.html\" target=\"_blank\">http:\/\/tldp.org\/LDP\/abs\/html\/aliases.html<\/a>\u00a0\u00a0#\u4e0d\u9519<\/li>\n<li><a href=\"http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Aliases.html\" target=\"_blank\">http:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Aliases.html<\/a>\u00a0\u00a0#\u5b98\u65b9\u8bf4\u660e<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1615877\/why-aliases-in-a-non-interactive-bash-shell-do-not-work\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1615877\/why-aliases-in-a-non-interactive-bash-shell-do-not-work<\/a>\u00a0\u00a0#\u7ed9\u529b<\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/1496\/why-doesnt-my-bash-script-recognize-aliases\" target=\"_blank\">http:\/\/unix.stackexchange.com\/questions\/1496\/why-doesnt-my-bash-script-recognize-aliases<\/a>\u00a0\u00a0#\u7ed9\u529b<\/li>\n<li><a href=\"https:\/\/www.shell-tips.com\/2007\/08\/25\/working-with-bash-aliases-aliasunalias\/\" target=\"_blank\">https:\/\/www.shell-tips.com\/2007\/08\/25\/working-with-bash-aliases-aliasunalias\/<\/a><\/li>\n<li><a href=\"http:\/\/fog.ccsf.edu\/~gboyd\/cs160b\/online\/8-advanced\/shopt.html\" target=\"_blank\">http:\/\/fog.ccsf.edu\/~gboyd\/cs160b\/online\/8-advanced\/shopt.html<\/a><\/li>\n<li><a href=\"http:\/\/askubuntu.com\/questions\/98782\/how-to-run-an-alias-in-a-shell-script\" target=\"_blank\">http:\/\/askubuntu.com\/questions\/98782\/how-to-run-an-alias-in-a-shell-script<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/15968053\/using-alias-in-shell-script\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/15968053\/using-alias-in-shell-script<\/a><\/li>\n<li><a href=\"http:\/\/superuser.com\/questions\/422122\/bash-ls-options-group-directories-first\" target=\"_blank\">http:\/\/superuser.com\/questions\/422122\/bash-ls-options-group-directories-first<\/a><\/li>\n<\/ul>\n<p>=EOF=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f18\u7531\uff1a \u7ecf\u5e38\u7528Linux\u8fdb\u884c\u64cd\u4f5c\u7684\u540c\u5b66\u4e00\u822c\u90fd\u4f1a\u6709\u5404\u79cd\u5404\u6837\u7684\u6280\u5de7\u6765\u63d0\u5347\u5de5\u4f5c\u6548\u7387\uff0c\u800c\u6dfb\u52a0\/\u6539\u5199shell\u4e2d\u7684ali [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7],"tags":[440,83],"class_list":["post-2357","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","tag-alias","tag-bash"],"views":5060,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2357","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2357"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2357\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}