passing arguments to alias in .bash_profile

Hello

I have defined this alias in my .bash_profile :

alias duk='du -k | sort -rn | grep -v "/.*/"'


This works fine if for the current working dir by just typing "duk" but when i pass any argument e.g. "duk abc/def " it doesn't print anything. How do i pass an argument in this alias ?

Thanks

PowerBook G4 15", Mac OS X (10.5.5), 1.5GHz/512MB/80GB

Posted on Nov 21, 2008 4:10 AM

Reply
7 replies

Nov 21, 2008 5:43 AM in response to kanny

When using an alias you don't have much control over how to re-order the arguments. Your typed-in arguments just get tacked on the end.

You have much more flexibility if you just create a new shell script and put it into a handy "bin" directory somewhere in your path. I have 36 such scripts.

Your new duk script would look something like this:

#!/bin/sh
du -k ${*} | sort -rn | grep -v "/.*/"

Nov 21, 2008 6:53 AM in response to kanny

This breaks though when the argument has space in it e.g. "This Folder" .

Change your $* to "$@" (see below):

duk() {
du -k "$@" | sort -rn | grep -v "$/./" | sed -e 's/[a-zA-Z0-9>*///'
}

The 4 character sequence "$@" has special shell magic. From the bash man page:
When the expansion occurs within double quotes,
each parameter expands to a separate word.
That is, "$@" is equivalent to "$1" "$2" "$3" ...

Nov 21, 2008 8:15 AM in response to kanny

I tried both $@ and ${*} and they don't address the problem of preserving the spaces in Folder names

[21:38 ~]$ duk Documents/Ulead Slide Show/
du: Documents/Ulead: No such file or directory
du: Slide: No such file or directory
du: Show/: No such file or directory


I think there was slight misunderstanding. I need to preserve the spaces in arguments while the suggestions seem to be for treating multiple arguments passed to the function. I read through the length of the bash man page, but still no avail. Any hints ?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

passing arguments to alias in .bash_profile

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.