{"id":2070,"date":"2015-04-18T13:32:42","date_gmt":"2015-04-18T05:32:42","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2070"},"modified":"2015-04-18T13:32:42","modified_gmt":"2015-04-18T05:32:42","slug":"%e9%9d%9e%e4%ba%a4%e4%ba%92%e5%bc%8fshell%e3%80%81%e9%9d%9e%e7%99%bb%e5%bd%95shell%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2070.html","title":{"rendered":"[\u975e]\u4ea4\u4e92\u5f0fshell\u3001[\u975e]\u767b\u5f55shell\u7684\u533a\u522b"},"content":{"rendered":"<p>Linux\u4e5f\u7b97\u662f\u7528\u4e86\u597d\u51e0\u5e74\u4e86\u5427\uff0cshell\uff08\u4e3b\u8981\u662fBash\uff09\u4e5f\u4e00\u76f4\u5728\u7528\uff0c\u6700\u5f00\u59cb\u5b66\u7684\u65f6\u5019\u5176\u5b9e\u770b\u5230\u8fc7\u4ea4\u4e92\u5f0fshell\u3001\u767b\u5f55shell\u7684\u8bf4\u660e\uff0c\u4f46\u5f53\u65f6\u4ec0\u4e48\u90fd\u4e0d\u61c2\uff0c\u7406\u89e3\u4e5f\u4e0d\u6df1\uff0c\u770b\u4e86\u548c\u6ca1\u770b\u533a\u522b\u4e0d\u592a\u5927\uff0c\u53ea\u662f\u8111\u5b50\u91cc\u7559\u4e0b\u4e86\u4e00\u4e2a\u5370\u8c61\u800c\u5df2\uff1b\u73b0\u5728\u65e0\u610f\u4e2d\u53c8\u78b0\u5230\u4e86\u8fd9\u4e2a\u8bdd\u9898\uff0c\u89c9\u5f97\u6709\u5fc5\u8981\u518d\u770b\u770b\uff0c\u8865\u5145\u4e00\u4e0b\u8fd9\u65b9\u9762\u7684\u77e5\u8bc6\u3002<\/p>\n<h6>\u641c\u7d22\u5173\u952e\u5b57\uff1a<\/h6>\n<p>linux \u767b\u5f55shell \u4ea4\u4e92\u5f0fshell<\/p>\n<p>himBh<\/p>\n<p>==<\/p>\n<h5>[\u975e]\u4ea4\u4e92\u5f0fshell\u3001[\u975e]\u767b\u5f55shell\u7684\u533a\u522b\uff1a<\/h5>\n<p>\u9996\u5148\uff0c\u8fd9\u662f\u4ece\u4e24\u4e2a\u4e0d\u540c\u7684\u7ef4\u5ea6\u6765\u5212\u5206\u7684\uff0c\u4e00\u4e2a\u662f\u662f\u5426\u4ea4\u4e92\u5f0f\uff0c\u53e6\u4e00\u4e2a\u662f\u662f\u5426\u9700\u767b\u5f55\u3002<\/p>\n<h6><strong>\u4ea4\u4e92\u5f0fshell\u548c\u975e\u4ea4\u4e92\u5f0fshell\uff08interactive shell and non-interactive shell\uff09<\/strong><\/h6>\n<p>\u4ea4\u4e92\u6a21\u5f0f\u5c31\u662f\u5728\u7ec8\u7aef\u4e0a\u6267\u884c\uff0cshell\u7b49\u5f85\u4f60\u7684\u8f93\u5165\uff0c\u5e76\u4e14\u7acb\u5373\u89e3\u91ca\u6267\u884c\u4f60\u63d0\u4ea4\u7684\u547d\u4ee4\u3002\u8fd9\u79cd\u6a21\u5f0f\u88ab\u79f0\u4f5c\u4ea4\u4e92\u5f0f\u662f\u56e0\u4e3a<span style=\"color: #ff0000;\"><strong>shell\u4e0e\u7528\u6237\u8fdb\u884c\u4ea4\u4e92<\/strong><\/span>\u3002\u8fd9\u79cd\u6a21\u5f0f\u4e5f\u662f\u5927\u591a\u6570\u7528\u6237\u975e\u5e38\u719f\u6089\u7684\uff1a\u767b\u5f55\u3001\u6267\u884c\u4e00\u4e9b\u547d\u4ee4\u3001\u9000\u51fa\u3002\u5f53\u4f60\u9000\u51fa\u540e\uff0cshell\u4e5f\u7ec8\u6b62\u4e86\u3002<\/p>\n<p>shell\u4e5f\u53ef\u4ee5\u8fd0\u884c\u5728\u53e6\u5916\u4e00\u79cd\u6a21\u5f0f\uff1a\u975e\u4ea4\u4e92\u5f0f\u6a21\u5f0f\uff0c\u4ee5shell script(\u975e\u4ea4\u4e92)\u65b9\u5f0f\u6267\u884c\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e0b\uff0cshell\u4e0d\u4e0e\u4f60\u8fdb\u884c\u4ea4\u4e92\uff0c\u800c\u662f\u8bfb\u53d6\u5b58\u653e\u5728\u6587\u4ef6\u4e2d\u7684\u547d\u4ee4\uff0c\u5e76\u4e14\u4f9d\u6b64\u89e3\u91ca\u6267\u884c\u5b83\u4eec\u3002\u5f53\u5b83\u8bfb\u5230\u6587\u4ef6\u7684\u7ed3\u5c3eEOF\uff0cshell\u4e5f\u5c31\u7ec8\u6b62\u4e86\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u201c$-\u201d\u53d8\u91cf\u7684\u503c\uff08\u4ee3\u8868\u7740\u5f53\u524dshell\u7684\u9009\u9879\u6807\u5fd7\uff09\uff0c\u67e5\u770b\u5176\u4e2d\u7684\u201ci\u201d\u9009\u9879\uff08\u8868\u793ainteractive shell\uff09\u6765\u533a\u5206\u4ea4\u4e92\u5f0f\u4e0e\u975e\u4ea4\u4e92\u5f0fshell\u3002<\/p>\n<p>\u67e5\u770bman bash\u4e2d\u7684Invocation\u4e00\u8282\u3002\u6709\uff1a<\/p>\n<blockquote><p><span style=\"color: #ff0000;\">A <strong>login shell<\/strong> is one whose first character of argument zero is a -, or one started with the &#8211;login option.<\/span><\/p>\n<p>An <span style=\"color: #ff0000;\"><strong>interactive shell<\/strong><\/span> is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state.<\/p><\/blockquote>\n<p>\u4e0a\u9762\u8fd9\u4e00\u6bb5\u5185\u5bb9\u5c31\u7ed9\u51fa\u4e86\u5982\u4f55\u533a\u5206\u767b\u5f55shell\u548c\u975e\u767b\u5f55shell\u3001\u4ea4\u4e92\u5f0fshell\u548c\u975e\u4ea4\u4e92\u5f0fshell\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre class=\"lang:default decode:true\">root@ixyzero.com:~# ps aux | grep bash\nroot      2513  0.0  0.7   7148  3604 pts\/0    Ss   12:39   0:00 -bash\nroot      3373  0.0  0.1   4672   820 pts\/0    S+   13:05   0:00 grep --color=auto bash\nroot@ixyzero.com:~# echo $0\n-bash\nroot@ixyzero.com:~# bash\nroot@ixyzero.com:~# echo $0\nbash\n\nroot@ixyzero.com:~# ps aux | grep bash\nroot      2513  0.0  0.7   7148  3604 pts\/0    Ss   12:39   0:00 -bash\nroot      3383  0.0  0.3   5784  2020 pts\/0    S    13:05   0:00 bash\nroot      3398  0.0  0.1   4672   820 pts\/0    S+   13:05   0:00 grep --color=auto bash\n\nroot@ixyzero.com:~# pstree -ps 2513\ninit(1)\u2500\u2500\u2500sshd(901)\u2500\u2500\u2500sshd(2425)\u2500\u2500\u2500bash(2513)\u2500\u2500\u2500bash(3383)\u2500\u2500\u2500pstree(3411)\nroot@ixyzero.com:~# pstree -ps 3383\ninit(1)\u2500\u2500\u2500sshd(901)\u2500\u2500\u2500sshd(2425)\u2500\u2500\u2500bash(2513)\u2500\u2500\u2500bash(3383)\u2500\u2500\u2500pstree(3417)\nroot@ixyzero.com:~# pstree -p 3383\nbash(3383)\u2500\u2500\u2500pstree(3426)\n\nroot@ixyzero.com:~# logout\nbash: logout: not login shell: use `exit'\nroot@ixyzero.com:~# exit\nexit\nroot@ixyzero.com:~# ps aux | grep bash\nroot      2513  0.0  0.7   7148  3604 pts\/0    Ss   12:39   0:00 -bash\nroot      3444  0.0  0.1   4672   820 pts\/0    S+   13:07   0:00 grep --color=auto bash\nroot@ixyzero.com:~# pstree -ps 2513\ninit(1)\u2500\u2500\u2500sshd(901)\u2500\u2500\u2500sshd(2425)\u2500\u2500\u2500bash(2513)\u2500\u2500\u2500pstree(3458)<\/pre>\n<pre class=\"lang:default decode:true \">root@ixyzero.com:~# cat ishell.sh\n#!\/bin\/bash\necho $-\nroot@ixyzero.com:~# bash ishell.sh\nhB\n\nroot@ixyzero.com:~# echo $-\nhimBH<\/pre>\n<h6><strong>\u767b\u5f55shell\u548c\u975e\u767b\u5f55shell<\/strong><\/h6>\n<p><span style=\"color: #ff0000;\">\u767b\u5f55shell\uff1a\u662f\u9700\u8981\u7528\u6237\u540d\u3001\u5bc6\u7801\u767b\u5f55\u540e\u624d\u80fd\u8fdb\u5165\u7684shell\uff08\u6216\u8005\u901a\u8fc7\u201c&#8211;login\u201d\u9009\u9879\u751f\u6210\u7684shell\uff09<\/span>\u3002<\/p>\n<p>\u975e\u767b\u5f55shell\uff1a\u5f53\u7136\u5c31\u4e0d\u9700\u8981\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\u5373\u53ef\u6253\u5f00\u7684Shell\uff0c\u4f8b\u5982\uff1a\u76f4\u63a5\u547d\u4ee4\u201cbash\u201d\u5c31\u662f\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u975e\u767b\u5f55shell\uff0c\u5728Gnome\u6216KDE\u4e2d\u6253\u5f00\u4e00\u4e2a\u201c\u7ec8\u7aef\u201d\uff08terminal\uff09\u7a97\u53e3\u7a0b\u5e8f\u4e5f\u662f\u4e00\u4e2a\u975e\u767b\u5f55shell\u3002<\/p>\n<p>\u6267\u884cexit\u547d\u4ee4\uff0c\u9000\u51fa\u4e00\u4e2ashell\uff08\u767b\u5f55\u6216\u975e\u767b\u5f55shell\uff09\uff1b<\/p>\n<p>\u6267\u884clogout\u547d\u4ee4\uff0c\u9000\u51fa\u767b\u5f55shell\uff08\u4e0d\u80fd\u9000\u51fa\u975e\u767b\u5f55shell\uff09\u3002<\/p>\n<pre class=\"lang:default decode:true \">root@ixyzero.com:~# echo $0\n-bash\nroot@ixyzero.com:~# bash\nroot@ixyzero.com:~# echo $0\nbash\nroot@ixyzero.com:~# logout\nbash: logout: not login shell: use `exit'\nroot@ixyzero.com:~# exit\nexit<\/pre>\n<p>\u5bf9\u4e8eBash\u6765\u8bf4\uff0c\u767b\u5f55shell\uff08\u5305\u62ec\u4ea4\u4e92\u5f0f\u767b\u5f55shell\u548c\u4f7f\u7528\u201c&#8211;login\u201d\u9009\u9879\u7684\u975e\u4ea4\u4e92shell\uff09\uff0c\u5b83\u4f1a\u9996\u5148\u8bfb\u53d6\u548c\u6267\u884c\/etc\/profile\u5168\u5c40\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u547d\u4ee4\uff0c\u7136\u540e\u4f9d\u6b21\u67e5\u627e~\/.bash_profile\u3001~\/.bash_login \u548c ~\/.profile\u8fd9\u4e09\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u8bfb\u53d6\u548c\u6267\u884c\u8fd9\u4e09\u4e2a\u4e2d\u7684\u7b2c\u4e00\u4e2a\u5b58\u5728\u4e14\u53ef\u8bfb\u7684\u6587\u4ef6\u4e2d\u547d\u4ee4\u3002\u9664\u975e\u88ab\u201c&#8211;noprofile\u201d\u9009\u9879\u7981\u6b62\u4e86\u3002<\/p>\n<p>\u5728\u975e\u767b\u5f55shell\u91cc\uff0c\u53ea\u8bfb\u53d6 ~\/.bashrc \uff08\u548c \/etc\/bash.bashrc\u3001\/etc\/bashrc \uff09\u6587\u4ef6\uff0c\u4e0d\u540c\u7684\u53d1\u884c\u7248\u91cc\u9762\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u89c4\u5219\uff0c\u53ef\u4ee5\u76f4\u63a5\u5728\u76f8\u5e94\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u52a0\u4e00\u4e9becho\u547d\u4ee4\u6765\u9a8c\u8bc1\u5176\u771f\u5b9e\u6027\u3002<\/p>\n<p>==<\/p>\n<h6><strong>\u73af\u5883\u53d8\u91cf{$-}\u4e2d\u5404\u4e2a\u5b57\u7b26\u7684\u542b\u4e49<\/strong><\/h6>\n<p>From <a href=\"http:\/\/linux.die.net\/man\/1\/bash\" target=\"_blank\">man bash<\/a>:<\/p>\n<blockquote><p>&#8211;<\/p>\n<p>Expands to the current option flags as specified upon invocation, by the <a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/The-Set-Builtin.html\" target=\"_blank\">set builtin<\/a> command, or those set by the shell itself (such as the -i option).<\/p><\/blockquote>\n<p>So these are the current options that control the behavior of the shell. In particular:<\/p>\n<ul>\n<li>h: Cache location of binaries in the $PATH. Speeds up execution, but fails if you move binaries around during the shell session.<\/li>\n<li>i: The current shell is interactive<\/li>\n<li>m: <a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Job-Control.html#Job-Control\" target=\"_blank\">Job control<\/a> is enabled<\/li>\n<li>B: <a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/Brace-Expansion.html#Brace-Expansion\" target=\"_blank\">Brace expansion<\/a> is enabled<\/li>\n<li>H: <a href=\"https:\/\/www.gnu.org\/software\/bash\/manual\/html_node\/History-Interaction.html#History-Interaction\" target=\"_blank\">History substitution<\/a> like !-1<\/li>\n<\/ul>\n<p>==<\/p>\n<h6>\u53c2\u8003\u94fe\u63a5\uff1a<\/h6>\n<ul>\n<li><a href=\"http:\/\/kodango.com\/what-is-interactive-and-login-shell\" target=\"_blank\">\u4ec0\u4e48\u662f\u4ea4\u4e92\u5f0f\u767b\u5f55 Shell<\/a><\/li>\n<li><a href=\"http:\/\/smilejay.com\/2012\/10\/interactive-shell-login-shell\/\" target=\"_blank\">\u4ea4\u4e92\u5f0fshell\u548c\u975e\u4ea4\u4e92\u5f0fshell\u3001\u767b\u5f55shell\u548c\u975e\u767b\u5f55shell\u7684\u533a\u522b<\/a><\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/trochiluses\/article\/details\/13767669\" target=\"_blank\">bash \u6df1\u5165\u7406\u89e3\uff1a\u4ea4\u4e92\u5f0fshell\u548c\u975e\u4ea4\u4e92\u5f0fshell\u3001\u767b\u5f55shell\u548c\u975e\u767b\u5f55shell\u7684\u533a\u522b<\/a><\/li>\n<li><a href=\"http:\/\/www.kryptosx.info\/archives\/977.html\" target=\"_blank\">Linux\u73af\u5883\u53d8\u91cf\u52a0\u8f7d\u7684\u7814\u7a76<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/bbs.chinaunix.net\/thread-1068678-1-1.html\" target=\"_blank\">linux\u4e0b\u7684bash\u4e0esh \u8be6\u89e3\u4ee5\u53ca\u4f8b\u5b50<\/a><\/li>\n<li><a href=\"http:\/\/bbs.chinaunix.net\/thread-2018339-1-1.html\" target=\"_blank\">\u767b\u5f55shell,\u4ea4\u4e92\u5f0f\u975e\u767b\u5f55shell,\u975e\u4ea4\u4e92\u5f0fshell<\/a><\/li>\n<li>=<\/li>\n<li><a href=\"http:\/\/kodango.com\/explain-shell-default-options\" target=\"_blank\">Shell \u9ed8\u8ba4\u9009\u9879 himBH \u7684\u89e3\u91ca<\/a><\/li>\n<li><a href=\"http:\/\/www.chainsawonatireswing.com\/2012\/02\/02\/find-out-what-your-unix-shells-flags-are-then-change-them\/\" target=\"_blank\">Find out what your UNIX shell\u2019s flags are &amp; then change them<\/a><\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/18146443\/what-do-the-characters-in-the-bash-environment-variable-mean\" target=\"_blank\">linux &#8211; What do the characters in the bash environment variable $- mean ? &#8211; Stack Overflow<\/a><\/li>\n<li><a href=\"http:\/\/unix.stackexchange.com\/questions\/129231\/what-does-i-mean\" target=\"_blank\">shell &#8211; What does &#8220;${-#*i}&#8221; != &#8220;$-&#8221; mean? &#8211; Unix &amp; Linux Stack Exchange<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u4e5f\u7b97\u662f\u7528\u4e86\u597d\u51e0\u5e74\u4e86\u5427\uff0cshell\uff08\u4e3b\u8981\u662fBash\uff09\u4e5f\u4e00\u76f4\u5728\u7528\uff0c\u6700\u5f00\u59cb\u5b66\u7684\u65f6\u5019\u5176\u5b9e\u770b\u5230\u8fc7\u4ea4\u4e92\u5f0fshel [&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],"tags":[487,65,488],"class_list":["post-2070","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","tag-himbh","tag-shell","tag-488"],"views":6395,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2070","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=2070"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2070\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}