{"id":3361,"date":"2017-04-01T16:29:38","date_gmt":"2017-04-01T10:59:38","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3361"},"modified":"2018-10-30T12:35:45","modified_gmt":"2018-10-30T07:05:45","slug":"php-shell_exec-vs-exec","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-shell_exec-vs-exec\/","title":{"rendered":"php shell_exec() vs exec()"},"content":{"rendered":"<h2 id=\"exec\"><span style=\"color: #800000;\">Exec:<\/span><\/h2>\n<ul>\n<li>exec \u2014 <strong>Execute an external program<\/strong><\/li>\n<\/ul>\n<h3 id=\"description\"><span style=\"color: #0000ff;\"><strong>Description:<\/strong><\/span><\/h3>\n[pastacode lang=\u201dphp\u201d manual=\u201dstring%20exec%20(%20string%20%24command%20%5B%2C%20array%20%26%24output%20%5B%2C%20int%20%26%24return_var%20%5D%5D%20)\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>exec()<\/strong> executes the given command.<\/p>\n<h3 id=\"example-1\"><span style=\"color: #339966;\"><strong>Example #1\u00a0<\/strong><\/span><\/h3>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%2F%2F%20outputs%20the%20username%20that%20owns%20the%20running%20php%2Fhttpd%20process%0A%2F%2F%20(on%20a%20system%20with%20the%20%22whoami%22%20executable%20in%20the%20path)%0Aecho%20exec(\u2018whoami\u2019)%3B%0A%3F%3E\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h2 id=\"shell_exec\"><span style=\"color: #800080;\">shell_exec<\/span><\/h2>\n<ul>\n<li>shell_exec \u2014 <strong>Execute command via shell and return the complete output as a string<\/strong><\/li>\n<\/ul>\n<h3 id=\"description-2\"><span style=\"color: #0000ff;\"><strong>Description:<\/strong><\/span><\/h3>\n[pastacode lang=\u201dphp\u201d manual=\u201dstring%20shell_exec%20(%20string%20%24cmd%20)%0A%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>This function is <strong>identical to the backtick<\/strong> <a href=\"https:\/\/www.wikitechy.com\/php\/php-operators\" target=\"_blank\" rel=\"noopener\">operator<\/a>.<\/p>\n<h3 id=\"example-2\"><span style=\"color: #339966;\"><strong>Example #2 :<\/strong><\/span><\/h3>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3C%3Fphp%0A%24output%20%3D%20shell_exec(\u2018ls%20-lart\u2019)%3B%0Aecho%20%22%3Cpre%3E%24output%3C%2Fpre%3E%22%3B%0A%3F%3E\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><span style=\"color: #3366ff;\"><strong>A couple of distinctions that weren\u2019t touched on here:<\/strong><\/span><\/p>\n<ul>\n<li>With this function, you can pass an optional param variable which will receive an array of output lines. In some cases this might save time, especially if the output of the <a href=\"https:\/\/www.wikitechy.com\/interview-questions\/php\/how-to-run-php-in-command-line\" target=\"_blank\" rel=\"noopener\">commands<\/a> is already tabular.<\/li>\n<\/ul>\n<h2 id=\"compare\"><span style=\"color: #ff6600;\">Compare:<\/span><\/h2>\n[pastacode lang=\u201dphp\u201d manual=\u201dexec(\u2018ls\u2019%2C%20%24out)%3B%0Avar_dump(%24out)%3B%0A%2F%2F%20Look%20an%20array%0A%24out%20%3D%20shell_exec(\u2018ls\u2019)%3B%0Avar_dump(%24out)%3B%0A%2F%2F%20Look%20\u2013%20a%20string%20with%20newlines%20in%20it\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<ul>\n<li>Conversely, if the output of the command is <strong><span style=\"color: #3366ff;\"><a style=\"color: #3366ff;\" href=\"https:\/\/www.wikitechy.com\/tutorials\/android\/xml-parsing-in-android\" target=\"_blank\" rel=\"noopener\">xml<\/a> <\/span>or<span style=\"color: #3366ff;\"> <a style=\"color: #3366ff;\" href=\"https:\/\/www.wikitechy.com\/step-by-step-tutorials\/json\/json-create-json-from-string-part1\" target=\"_blank\" rel=\"noopener\">json<\/a><\/span><\/strong>, then having each line as part of an array is not what you want, as you\u2019ll need to post-process the input into some other form, so in that case use shell_exec.<\/li>\n<li>It\u2019s also worth pointing out that shell_exec is an alias for the <strong>backtick operator<\/strong>, for those used to <strong>*nix.<\/strong><\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24out%20%3D%20%60ls%60%3B%0Avar_dump(%24out)%3B\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>It also supports an additional parameter that will provide the return code from the executed command:<\/li>\n<\/ul>\n[pastacode lang=\u201dphp\u201d manual=\u201dexec(\u2018ls\u2019%2C%20%24out%2C%20%24status)%3B%0Aif%20(0%20%3D%3D%3D%20%24status)%20%7B%0A%20%20%20%20var_dump(%24out)%3B%0A%7D%20else%20%7B%0A%20%20%20%20echo%20%22Command%20failed%20with%20status%3A%20%24status%22%3B%0A%7D\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>As noted in the <a href=\"https:\/\/www.wikitechy.com\/tutorials\/linux\/how-to-use-shell-variables-in-an-awk-script-in-linux\" target=\"_blank\" rel=\"noopener\">shell_exec<\/a> manual page, when you actually require a return code from the command being executed, you have no choice but to<strong> use exec<\/strong>.<\/p>\n<h2 id=\"difference\"><span style=\"color: #800080;\">Difference:<\/span><\/h2>\n<ul>\n<li>shell_exec \u2013 <strong>Execute command via shell<\/strong> and return the complete output as a string<\/li>\n<li>exec \u2013 <strong>Execute an external program<\/strong>.<\/li>\n<\/ul>\n<p>The difference is that with shell_exec you get <strong>output as a return value<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p> php shell_exec() vs exec() With exec(), you can pass an optional param variable which will receive an array of output lines.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[6248,6246,6247,6251,6249,6250,6252,2392,6245,2393,404,6243,6242,6244],"class_list":["post-3361","post","type-post","status-publish","format-standard","hentry","category-php","tag-and-system-returning-only-partial-output","tag-exec-and-shell_exec-not-working-in-php","tag-exec","tag-how-to-output-from-a-php-script-called-by-exec-or-shell_exec","tag-how-to-run-casperjs-script-without-using-php-exec-or-shell_exec","tag-path-is-not-available-in-php-exec-or-shell_exec","tag-php-shell_exec-and-exec-returning-false-when-ran","tag-php-exec","tag-php-exec-and-shell_exec","tag-php-shell_exec","tag-reference-what-does-this-symbol-mean-in-php","tag-shell_exec","tag-shell_exec-throwing-warning-php","tag-system-not-working"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3361"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3361\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}