How can I make cat someFile | ssh someHost work when someFile is not a bash script? I want to remotely execute a perl script but I get a bunch of syntax errors from bash when I try the cat | ssh command.

3 Answers 3


If you want to push the Perl script through the SSH connection, you'll have to run the Perl interpreter on the remote end. It'll read the script from stdin:

ssh remotehost perl < somescript.pl

In the case of Perl, it should even read the command line switches (except -T) from the hashbang line of the input.

If you want to give command line arguments to the Perl interpreter, you can just add them to the command line after perl. If you want to give arguments to the script, you'll need to explicitly tell the interpreter to read the script from stdin (otherwise it will take the first argument as a file name to look for).

So, here -l goes to the interpreter, and foo and bar to the script:

echo 'print "> $_"  foreach @ARGV' | ssh remotehost perl -l - foo bar 

Note that doing just ssh somehost < script.sh counts on the remote login shell being compatible with the script. (i.e. a Bash script won't work if the remote shell happens to be something else.)

  • I've found that cat perlScript | ssh remoteHost perl works great. I'd like to pass a command line argument to perl. Do you know how I might do that?
    – Sol
    Mar 22, 2017 at 17:30
  • @Sol, edited, since there's a slight difference between switches to perl and what goes to the script's @ARGV
    – ilkkachu
    Mar 22, 2017 at 17:59
  • A friend suggested this and it works great! cat perlScript | ssh remoteHost perl /dev/stdin someArgument The /dev/stdin part is most interesting.
    – Sol
    Mar 22, 2017 at 18:18

ssh someHost will execute your default shell, which in turn will execute the commands it reads from standard input. Since you are sending the content of someFile to standard input, it logically tries to executes to content of someFile.

You don't tell us what someFile is supposed to contain. Assuming its content is supposed to be fed to your perl script, what you should do is:

cat someFile | ssh someHost your_perl_script

Or better:

ssh someHost your_perl_script < someFile
  • someFile is the perl script I want to execute remotely. Sorry I wasn't clear in the problem description.
    – Sol
    Mar 22, 2017 at 17:32

in ordred to =>

Write local output into remote file: You just installed the apache on your $myhost. Now you want to test it.

You can echo the test string into file, only this time the file is in remote htdocs root:

echo "It works" | ssh [email protected] 'cat >> /usr/local/apache/htdocs/it_works.html'

Open remote file with less or cat: With cat:

ssh [email protected] "cat /usr/local/apache/htdocs/it_works.html"

With less:

ssh [email protected] "less /usr/local/apache/htdocs/it_works.html"

Grep remote files:

With pipe:

ssh [email protected] "cat /usr/local/apache/htdocs/it_works.html" |  grep "works"

Just remote grep:

ssh [email protected] "grep works /usr/local/apache/htdocs/it_works.html"

Watching remote logs with tail:

This way you can watch the access to apache server in real-time:

 ssh [email protected] "tail -f /etc/httpd/logfiles/access_log"

The source for this answer is here.

  • 2
    Running less on the remote like that is a bit bare, since by default SSH (without -t) doesn't allocate a terminal if a command line is given, so less only sees a socket, and most of its functionality is lost.
    – ilkkachu
    Mar 22, 2017 at 17:04
  • 1
    Does any of this answer the question here, anyway?
    – ilkkachu
    Mar 22, 2017 at 17:04
  • I'm trying to remotely execute a perl script. I think this may not be possible without copying the perl script to the remote machine.
    – Sol
    Mar 22, 2017 at 17:18
  • check @ilkkachu answer.
    – Kingofkech
    Mar 22, 2017 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .