LRK9
LRK9

Reputation: 351

My bash script on my MacBook can not get the stdout of grep

I have tried a dozen variations on this, without getting results:

#!/usr/bin/env bash
echo $1;
history | grep $1  | cat > /tmp/hsg_output ;
cat /tmp/hsg_output;

I create this on my MacBook Pro. I store it as /usr/local/bin/hsg. I chmod it 0777. If I run:

which hsg

Then I see:

/usr/local/bin/hsg

This line correctly outputs my argument:

echo $1;

If, in my terminal window, I run this line on its own:

history | grep upstream  | cat > /tmp/hsg_output ;

Then I can:

cat /tmp/hsg_output

And I see what I expect, which is a history of every time I've pushed my local git repo to its upstream repo.

But when I run this script, nothing appears in /tmp/hsg_output .

Actually, /tmp/hsg_output gets overwritten every time I run "hsg". So the script is correctly writing to /tmp/hsg_output, but it has nothing to write.

I've also tried:

history | grep $1  > /tmp/hsg_output ;

Which also fails to work (or rather, writes an empty stdin to the the file.

Why am I not able to capture the stdout of grep?

Upvotes: 0

Views: 60

Answers (1)

strobelight
strobelight

Reputation: 267

The bash running your script is considered non-interactive, so the history command is silently ignored.

If you really want this functionality:

#!/bin/bash
echo $1
grep $1 $HISTFILE > /tmp/hsg_output

Upvotes: 1

Related Questions