Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

command line history?

by brp4h (Acolyte)
on Dec 10, 2009 at 17:08 UTC ( #812244=perlquestion: print w/replies, xml ) Need Help??

brp4h has asked for the wisdom of the Perl Monks concerning the following question:

Help! Why don't I get any output for this?
$hist = `history`; print $hist;

Replies are listed 'Best First'.
Re: command line history?
by almut (Canon) on Dec 10, 2009 at 18:17 UTC

    For bash:

    #!/usr/bin/perl $hist = `bash -ic 'set -o history; history'`; print $hist;

    (don't ask why set -o history and -i (i.e. interactive) are required... in theory, either option on its own should enable the history)

    But note that although this prints a history, it might not be the history you're looking for (the one of the current shell instance that you run this Perl script from(?)). Rather, it's just the contents of the shell's history file (typically ~/.bash_history), which the new shell instance reads upon startup. This file holds whatever history log was last written to it (normally, when some interactive shell exits).  In other words, unless you execute history -w from within the current shell instance prior to running the script, you won't get the current status of its history buffer, which is being held in memory only, otherwise...

Re: command line history?
by ikegami (Pope) on Dec 10, 2009 at 17:26 UTC
    Perhaps you should check what error you got!
    $ perl -e'print `history` // die($? == -1 ? $! : "\$?=$?")' No such file or directory at -e line 1.

    history is a shell command. Just like you can't execute a line of Perl code without loading Perl, you can't execute a line of sh code without loading sh. Here's how you can do that:

    $ perl -e'sh -c print `history` // die($? == -1 ? $! : "\$?=$?")' $

    Unfortunately, that doesn't print anything either. But that's the shell doing that:

    $ sh -c history $

    I tried a few things quick, but they didn't work. How to fix is is beyond my knowledge and it has nothing to do with Perl. Consult your shell's documentation.

Re: command line history?
by toolic (Bishop) on Dec 10, 2009 at 17:17 UTC
    Why don't I get any output for this?
    You will get output if you use strict and warnings. Then, get more info with diagnostics. It won't be the output you want to get, but you will get warning messages.

    Perhaps this does not work because history is a shell built-in command which is not in the PATH environment variable.

    Update: As a potential workaround, it seems that some shells support a history file. Perhaps it is as simple as setting an environment variable (HISTFILE?) in your shell, then parsing that file inside your Perl code.

      You could read in the history file, e.g. .bash_history or whatever file for shell you're using.

      Yah, but.. Why *doesn't* that output history? Does the environment perl runs under not have access to that? Very neat.
Re: command line history?
by Fox (Pilgrim) on Dec 10, 2009 at 17:27 UTC
    toolic meant history is a shell built-in, like a bash arg, there's no program history

    try  which history
      which history comes back with "no history in <list of PATH variables>"
        exactly, just proving that you can't execute history because there is no such program with this name.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://812244]
Approved by toolic
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2020-10-19 21:19 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (207 votes). Check out past polls.