Vivek Verma
Vivek Verma

Reputation: 1

What is wrong this simple history script?

I am missing something really simple I think:

$ cat hs.sh
#!/bin/bash
echo $1
history | grep -i $1
echo $#
exit
$

here is output:

$ ./history_search sed
sed
1
$

Trying to create a script which I can use in form of './hs.sh sed' to search for all sed commands in history. I can create an alias using this which works fine, but not this script.

Here is the alias:

alias hg='history | grep -i $1'

Upvotes: 0

Views: 58

Answers (2)

EJK
EJK

Reputation: 12527

When you run this as a shell script, it spawns a new shell that has no history.

Try running it in the same shell like this:

source ./history_search see

and it should work.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753695

Interactive shells have history; scripted shells do not have history. You can only ask for history from an interactive shell, which is why the alias works but the script does not.

Upvotes: 2

Related Questions