Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use commandline arguments in fish shell scripts

Tags:

shell

fish

I'm trying to write an alias for grep:

# config.fish
alias grepcustom="grep -r $argv ~/"

Basically what I want this to do is a recursive grep on my home directory without having to type everything out (i know im lazy).

When I restart the shell and run grepcustom I get:

$ grepcustom "hello"
grep: hello: No such file or directory

There is a file that contains "hello". If I run grep -r in the shell it works properly. However, the problem seems to be with how my alias identifies the commandline argument "hello". What am I doing wrong?

like image 771
dopatraman Avatar asked Apr 25 '26 18:04

dopatraman


1 Answers

You're going to need to write an actual function:

$ alias grepcustom="grep -r $argv ~/"
$ type grepcustom
grepcustom is a function with definition
function grepcustom
    grep -r  ~/ $argv;
end

What happened there? Since I have not defined $argv in my shell, it's replaced by an empty string, and then alias added $argv to make the function work. Let's try with single quotes:

$ alias grepcustom='grep -r $argv ~/'
$ type grepcustom
grepcustom is a function with definition
function grepcustom
    grep -r $argv ~/ $argv;
end

That's clearly not right. You want

function grepcustom -a pattern
    grep -r $pattern ~/
end
like image 55
glenn jackman Avatar answered Apr 28 '26 11:04

glenn jackman