I wrote simple command that lets me run the last N commands from terminal history. It looks like this: $> r 3
which will replay the last 3 commands.
I have the following alias in my bash profile:
alias r="history -w; runlast $1"
And then the following simple perl script for the runlast
command:
#!/usr/bin/env perl
use strict;
use warnings;
my $lines = $ARGV[0] || exit;
my @last_commands = split /\n/,
`bash -ic 'set -o history; history' | tail -$lines`;
@last_commands =
grep { $_ !~ /(^r |^history |^rm )/ }
map { local $_ = $_; s/^\s+\d+\s+//; $_ }
@last_commands;
foreach my $cmd (@last_commands) {
system("$cmd");
}
This works but my bash profile has aliases and other features (e.g. color output) I want the perl script to have access to. How do I load the bash profile for perl so it runs the bash commands with my bash profile? I read somewhere that if you "source the bash profile" for perl you can get it to work. So I tried adding source ~/.bash_profile;
to my r
command alias but that didn't have an effect. I'm not sure if I was doing that correctly, though.
$1
in the alias definition doesn't mean what you hope it means. Anyway, the only thing you really need to know about aliases is, use functions instead. – tripleee