<p>In Perl, you can execute system commands using system() or `` (backticks). You can even capture the output of the command into a variable. However, this hides the program execution in the background so that the person executing your script can't see it. </p>

<p>Normally this is useful but sometimes I want to see what is going on behind the scenes. How do you make it so the commands executed are printed to the terminal, and those programs' output printed to the terminal? This would be the <code>.bat</code> equivalent of "@echo on".</p>

[{'answer_id': 17245, 'author': 'mk.', 'author_id': 1797, 'author_profile': 'https://Stackoverflow.com/users/1797', 'pm_score': 3, 'selected': False, 'text': '<p>Use open instead. Then you can capture the output of the command.</p>\n\n<pre><code>open(LS,"|ls");\nprint LS;\n</code></pre>\n'}, {'answer_id': 17293, 'author': 'Daniel Fone', 'author_id': 1848, 'author_profile': 'https://Stackoverflow.com/users/1848', 'pm_score': 4, 'selected': True, 'text': '<p>As I understand, system() will print the result of the command, but not assign it. Eg.</p>\n<pre><code>[daniel@tux /]$ perl -e \'$ls = system(&quot;ls&quot;); print &quot;Result: $ls\\n&quot;\'\nbin   dev  home  lost+found  misc  net  proc  sbin     srv  System  tools  var\nboot  etc  lib   media       mnt   opt  root  selinux  sys  tmp     usr\nResult: 0\n</code></pre>\n<p>Backticks will capture the output of the command and not print it:</p>\n<pre><code>[daniel@tux /]$ perl -e \'$ls = `ls`; print &quot;Result: $ls\\n&quot;\'\nResult: bin\nboot\ndev\netc\nhome\nlib\n</code></pre>\n<p>etc...</p>\n<p><strong>Update:</strong> If you want to print the name of the command being <code>system()</code> \'d as well, I think <a href="https://stackoverflow.com/questions/17225/echo-system-commands-to-terminal-in-perl#17304">Rudd</a>\'s approach is good. Repeated here for consolidation:</p>\n<pre><code>sub execute {\n    my $cmd = shift;\n    print &quot;$cmd\\n&quot;;\n    system($cmd);\n}\n\nmy $cmd = $ARGV[0];\nexecute($cmd);\n</code></pre>\n'}, {'answer_id': 17304, 'author': 'Rudd Zwolinski', 'author_id': 219, 'author_profile': 'https://Stackoverflow.com/users/219', 'pm_score': 4, 'selected': False, 'text': '<p>I don\'t know of any default way to do this, but you can define a subroutine to do it for you:</p>\n\n<pre><code>sub execute {\n    my $cmd = shift;\n    print "$cmd\\n";\n    system($cmd);\n}\n\nmy $cmd = $ARGV[0];\nexecute($cmd);\n</code></pre>\n\n<p>And then see it in action:</p>\n\n<pre><code>pbook:~/foo rudd$ perl foo.pl ls\nls\nfile1   file2   foo.pl\n</code></pre>\n'}, {'answer_id': 17331, 'author': 'jj33', 'author_id': 430, 'author_profile': 'https://Stackoverflow.com/users/430', 'pm_score': 2, 'selected': False, 'text': '<p>Hmm, interesting how different people are answering this different ways.  It looks to me like <a href="https://stackoverflow.com/questions/17225/echo-system-commands-to-terminal-in-perl#17245">mk</a> and <a href="https://stackoverflow.com/questions/17225/echo-system-commands-to-terminal-in-perl#17293">Daniel Fone</a> interpreted it as wanting to see/manipulate the stdout of the command (neither of their solutions capture stderr fwiw).  I think <a href="https://stackoverflow.com/questions/17225/echo-system-commands-to-terminal-in-perl#17304">Rudd</a> got closer.  One twist you could make on Rudd\'s response is to overwite the built in system() command with your own version so that you wouldn\'t have to rewrite existing code to use his execute() command.</p>\n\n<p>using his execute() sub from Rudd\'s post, you could have something like this at the top of your code:</p>\n\n<pre><code>if ($DEBUG) {\n   *{"CORE::GLOBAL::system"} = \\&amp;{"main::execute"};\n}\n</code></pre>\n\n<p>I think that will work but I have to admit this is voodoo and it\'s been a while since I wrote this code.  Here\'s the code I wrote years ago to intercept system calls on a local (calling namespace) or global level at module load time:</p>\n\n<pre><code>  # importing into either the calling or global namespace _must_ be\n  # done from import().  Doing it elsewhere will not have desired results.\n  delete($opts{handle_system});\n  if ($do_system) {\n    if ($do_system eq \'local\') {\n      *{"$callpkg\\::system"} = \\&amp;{"$_package\\::system"};\n    } else {\n      *{"CORE::GLOBAL::system"} = \\&amp;{"$_package\\::system"};\n    }\n  }\n</code></pre>\n'}, {'answer_id': 17468, 'author': 'Greg Hewgill', 'author_id': 893, 'author_profile': 'https://Stackoverflow.com/users/893', 'pm_score': 2, 'selected': False, 'text': '<p>Another technique to combine with the others mentioned in the answers is to use the <code>tee</code> command. For example:</p>\n\n<pre><code>open(F, "ls | tee /dev/tty |");\nwhile (&lt;F&gt;) {\n    print length($_), "\\n";\n}\nclose(F);\n</code></pre>\n\n<p>This will both print out the files in the current directory (as a consequence of <code>tee /dev/tty</code>) and also print out the length of each filename read.</p>\n'}, {'answer_id': 18328, 'author': 'andrewrk', 'author_id': 432, 'author_profile': 'https://Stackoverflow.com/users/432', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s an updated execute that will print the results and return them:</p>\n\n<pre><code>sub execute {\n  my $cmd = shift;\n  print "$cmd\\n";\n  my $ret = `$cmd`;\n  print $ret;\n  return $ret;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/17225', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/432/']