Alias problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alias problem
# 1  
Old 04-16-2013
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:
Code:
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:
Code:
ff x*

and it finds all the files beginning with x recursed through all the subdir. If I issue the command:
Code:
ff x* -ls

then it list all files with detailed file information. However if I change the alias like this:
Code:
alias ff='find . -name $1 -ls'

and then issue the command ff x*, I get the following error:
Code:
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.

Last edited by Franklin52; 04-16-2013 at 10:52 AM.. Reason: Please use code tags
# 2  
Old 04-16-2013
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.
# 3  
Old 04-16-2013
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 ?
# 4  
Old 04-16-2013
Function is more usable as alias.
Where to put own function ?

Example make file $HOME/myfunc and then add line
Code:
. $HOME/myfunc

to $HOME/.profile

After that you can use function as command with arguments
Code:
# myfunc
ff()
{
  find . -name $1
}

Test it
Code:
.  ./myfunc
ff myfunc

If you like to use ff also in subprocess, then set ENV variable in .profile
Code:
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.

Last edited by kshji; 04-16-2013 at 12:25 PM..
# 5  
Old 04-16-2013
Thank you kshji.

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

Code:
ff()
{
  find . -name $1
}

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

Last edited by Corona688; 04-16-2013 at 02:20 PM..
# 6  
Old 04-16-2013
You do not. .profile is actually an appropriate place to put it. Smilie
# 7  
Old 04-17-2013
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:
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Facing problem with Alias created through script.

Hi Guru's, I am creating alias for db instance running on a server through script, am able to create them based on /etc/oratab entries and can use successfully with the below script. #!/bin/bash SCRIPT_PATH=${HOME}/scripts/db/script... (3 Replies)
Discussion started by: venky.b5
3 Replies

2. Red Hat

alias ip problem,i canīt find out solution

hi, we have eth1; in the beginning it was proposed to create from eth1, eth1.2500 and eth1.240. It did not worked cause vlan 1.2500 was not tagged in the switch. so they decided to leave eth1 like a physical interface with eth1.2500 config (removel vlan tag) and create eth1.240 but it did... (0 Replies)
Discussion started by: pabloli150
0 Replies

3. UNIX for Dummies Questions & Answers

Create alias files (not alias commands)

If one: $ find -name 'some expression' -type f > newfile and then subsequently wants to create an alias file from each pathname the find command retrieved and the > placed within 'newfile', how would one do this? Ideally, the newly created alias files would all be in one directory. I am... (3 Replies)
Discussion started by: Alexander4444
3 Replies

4. Shell Programming and Scripting

Alias problem with awk command

Hi to all, I'm facing some problems when adding an alias like: #alias list="ls -al | awk '{ print $1, $2, $3, $4, (($5/1048576))"\t", $6, $7, $8, $9 }'" and when I enter: #list I get: Syntax Error The source line is 1. The error context is { print >>> , <<< awk:... (3 Replies)
Discussion started by: enux
3 Replies

5. Linux

mail alias problem

Hi all, well i am configuring backuppc for awhile, anyway my current problem is, i set a smarthost, configured the mail to use our ISP smtp server, and its working successfully thanks to ALLAH, now i want to set an alias for mails( it was working when it was using the local smtp server), so the... (0 Replies)
Discussion started by: XP_2600
0 Replies

6. Shell Programming and Scripting

alias defining problem in .cshrc file

Hi folks, I'm trying to define the following command as alias in .cshrc file: ls -ltr | grep ^d | awk '{print $9}' | xargs du -hs I defined it as the following: alias nirdirs '`ls -ltr | grep "^d" | awk "{print \\$9}" | xargs du -hs`' I've got the following error when I've run the alias:... (7 Replies)
Discussion started by: nir_s
7 Replies

7. OS X (Apple)

Problem using a top command alias in my profile

Hi- I am newish to the mac osx unix interface. I want to set up top so that it always displays the username. I can use this command to do this: top -ocpu -P ' PID COMMAND %CPU TIME #TH #PRTS #MREGS RPRVT RSHRD RSIZE VSIZE USER' -p '$aaaa ^bbbbbbbbb $cccc $wwwwwww $ee... (1 Reply)
Discussion started by: mikey11415
1 Replies

8. Shell Programming and Scripting

Problem with alias and invisible text

okay how do i make an alias that has a space in it? for most of my other ones i've simply done within my ~/.bash_profile alias `ls`='ls -laF' but with alias `sudo su`='sh hello.sh' I'm unable to make it work in addition i was wondering how i could allow the user to type in... (1 Reply)
Discussion started by: cleansing_flame
1 Replies

9. UNIX for Dummies Questions & Answers

Alias problem

i'm trying to get the alias alias "sudo su"=`sh script.sh` whats wrong with this and how can i make an alias that has a space in it (1 Reply)
Discussion started by: cleansing_flame
1 Replies

10. UNIX for Dummies Questions & Answers

alias problem

How can I get two commands in one alias? In my exaple it only display the current date. alias two='cal | date' (5 Replies)
Discussion started by: Mattiax
5 Replies
Login or Register to Ask a Question