LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 11-25-2006, 06:35 AM   #1
stabu
Member
 
Registered: Mar 2004
Location: dublin IRL
Distribution: Slackv12.1, Slamd64v12.1,Xubuntu v8.10_64, FC8_64
Posts: 438
Blog Entries: 5

Rep: Reputation: 32
argument variables in .bashrc's aliases?


Hi,

[I recognise this is a bash question, but I'd like to limit it to Slack users, forgive if it trangresses etiquette]

My .bashrc file is full of aliases and I carry it around on my USB, it's become pretty much indispensible to me.

In effort to make even more use of them, I was wondering if I could use the arguments that come after the alias-command within the alias as defined in the .bashrc.

I mean, take tar. You want to tar up a current directory tree quickly, but you want to be able to specify the name of the tar file each time (i.e. by putting it as an argument to the alias). Is it possible to have the alias recognise the argument that comes after it and so use it as the tar file name?

What I do now, is write an alias which uses a generic name for the tarfile and then I rename it after using the alias. Of course, this is straightforward in a small script, but an alias? Actually,

alias tart='tar file.tar * && mv file.tar'

does appear to work. Though it complains "tar: file.tar: file is the archive; not dumped"

I suppose that I expect that aliases are really just expansions, and that this is not possible. I expect you need to include a function (maybe) in the .bashrc.

Another way could be write out the name of the tarfile you want to use on the prompt line and press return. THis would sotr eit in the !$ expansion. Then the alias might recognise the !$ expansion. However, that's unelegant. [ehem, just tried it, alias won't expand the !$. Now, that's a sign of their being pretty elementary tools.]

Cheers!
 
Old 11-25-2006, 06:56 AM   #2
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
I'm no expert so maybe this isn't what your after, but here goes anyway:

The arguments after any command are refered to as $1 $2 $n for nth argument, so:

alias play="mp3blaster $1"

would take one argument.

then,

play /home/chris/Music

would be equivilent to

mp3blaster /home/chris/Music

nb: the same thing works for functions too, of course, but is not exclusive to them.

does that help? I'm still quite new to linux so forgive me if I've missed the point!!!

Last edited by ChrisScott; 11-25-2006 at 05:46 PM.
 
Old 11-25-2006, 07:25 AM   #3
duryodhan
Senior Member
 
Registered: Oct 2006
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054

Rep: Reputation: 46
thats quite correct.
If you want even more functionality try making it a function

then something like this would work

tart()
{
tar $1 *
mv $1
echo "DONE"
return 0
}

You get the gist? the main thing you wanted was I guess the fact that $1 represents the arg
I couldn't quite understand what you wanted to do there so I guess my function will be wrong.
 
Old 12-15-2006, 11:30 AM   #4
bigqueso
LQ Newbie
 
Registered: Jun 2006
Location: Salt Lake City, UT, USA
Distribution: Ubuntu (laptop,home), Suse (work)
Posts: 8

Rep: Reputation: 0
@ChrisScott

I was trying to get a similar thing to work with a mkdir/cd compound command, and $1 just doesn't work. Take for example:

$ alias mcd="echo +$1+;echo +$1+"

$ mcd test
++
++ test

I had to make a function out of it to get it to work properly:

$ mcd() { echo +$1+; echo +$1+; }

$ mcd test
+test+
+test+

$ mcd() { mkdir -p $1; cd $1; }

bigler@homemachine ~
$ mcd test

bigler@homemachine ~/test
$
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sendmail and NIS databases (aliases, mail.aliases) - what kind of databases? cotton213 Linux - Software 0 03-14-2006 05:57 PM
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
See aliases gubak Linux - Newbie 0 08-04-2004 06:13 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
what aliases do you use most? m0rl0ck Linux - General 17 02-12-2003 01:25 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration