Alias problem

Please forgive me if this is too stupid a question as I am fairly new to scripting.

I have an alias in my .profile (ksh) like this:

alias ff='find . -name $1'

The idea is to find all files in current dir and all subdir with the name specified in the param passed. So, I issue the command:

ff x*

and it finds all the files beginning with x recursed through all the subdir. If I issue the command:

ff x* -ls

then it list all files with detailed file information. However if I change the alias like this:

alias ff='find . -name $1 -ls' 

and then issue the command ff x*, I get the following error:

find: bad option x*
find: [-H | -L] path-list predicate-list

I am just wondering why the same option works fine from the command line but not when added to the alias definition in .profile.

Thanks for the help.
Subhash.

An alias does not use command line parameters. It is replaced by its definition, and then the rest of your command line is expanded and appended to it. $1 in your case is not the second word on the command line but the first positional parameter of your interactive shell.
To enable passing of parameters, use a shell function.

Thanks RudiC for the reply.

So, if I understand correctly, the $1 in this case does nothing. When I issue the command "ff x*", it is expanded as "find . $1 x*" where the system looks for any files that have a name "$1" or starts with x.

Is that correct ?

Function is more usable as alias.
Where to put own function ?

Example make file $HOME/myfunc and then add line

. $HOME/myfunc

to $HOME/.profile

After that you can use function as command with arguments

# myfunc
ff()
{
  find . -name $1
}

Test it

.  ./myfunc
ff myfunc

If you like to use ff also in subprocess, then set ENV variable in .profile

ENV=$HOME/myfunc
export ENV

After this ENV setting ksh will execute myfunc always when you start ksh process => all function in myfunc is usable in subprocess.

Other method is use FPATH, but I like more "one file library" method. FPATH every function is in own file and filename is same as function name.

Thank you kshji.

I took a shortcut and placed the function in .profile itself. Like this:

ff()
{
  find . -name $1
}

That seems to work. Do I need to create a separate file $HOME/myfunc as you suggested ?

You do not. .profile is actually an appropriate place to put it. :slight_smile:

Of course every script, also .profile can include function. But if you like function working like alias = global then you need to set ENV and make some file which have to read every time when you start sh process. Usually it has named .kshrc. In my solution I have only show that name can be any. "Standard" is to use .shrc or .kshrc. Bash use .bashrc.

If you like function is usable also in child process then add to the .profile:

ENV=$HOME/.kshrc
export ENV

And make file .kshrc which include ex. those function which you like to be global.

But if you like to put together this need with ksh and bash, then make $HOME/.bashrc
and set ENV=$HOME/.bashrc + export in the profile.
=> function and all other settings are global in the bash and ksh93.

FPATH and autoload is the other method to make function working also in child process. = Autoload function when shell is launched.

I have not seen any command to set function to be global. Compare export for the variables.

Many shells read .profile too. If it's working for him, then it's working for him.

I don't know any shells which use .profile when you launch shell.

Profiles are used only in login process or example when use su with option -.
=> if you like your variables are global, you need to make global (export cmd) and for function you need to make it using .bashrc in bash using end set ENV=file in ksh or dash using.

You can test it: when you have logged in, give the command:

bash
# now try your function which you have in .profile, not work or /etc/profile,  ~/.bash_profile, ~/.bash_login
# but if function is in the .bashrc or in ksh/dash ENV=filename file, then it works

But if it's enough that it works only in current process, then .profile is okay. But if you like to use function in subprocess, then .profile is wrong place, it not work in child process.