Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
09-13-2005, 12:53 AM
|
#1
|
LQ Newbie
Registered: Sep 2005
Distribution: Fedora
Posts: 1
Rep:
|
alias cs='cd $1; ls'
Hi,
I am stuck here, I don't know why this alias doesn't work:
alias cs='cd $1; ls'
When I type cs new_dir, I want to list contents of the new_dir and STAY IN IT. But it always takes me back to the original directory after listing. How can I fix this and, more importantly, WHY doesn't it work?
Thanks for your help.
|
|
|
09-13-2005, 03:17 AM
|
#2
|
Senior Member
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606
Rep:
|
Hi, not sure alias can take parameters like $1
I struggled having something a bit similar to what you are doing.
I eventually defined a function within .bashrc
(cannot remember if I then aliased the function itself)
Am in no way an expert, so add a pinch of salt
|
|
|
09-13-2005, 06:20 AM
|
#3
|
Senior Member
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851
Rep:
|
Aliases cannot use arguments as per the above. A better solution would be to write a shell script like so:
Code:
#!/bin/bash
cd $1
ls
#EOF
Use it in the same manner, and call it cs. Make it executable, and stick it in /usr/bin
|
|
|
09-13-2005, 07:34 AM
|
#4
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
No need to create a script. As said above, in the file where you want to create your alias, create a function instead:
Code:
function cs() {
cd "$1"; ls
}
You could even refine that a bit, by allowing your function to take parameters for ls:
Code:
function cs() {
cd "$1"
shift
ls "$@"
}
With this function, you could do for example:
Code:
~$ cs some_dir/ -aF
./ gest-fichiers.lib.sh* logs.sh* mkAll.sh* stats.sh* wlsLinks.sh*
../ ieLinks.sh* menuV1.sh* open* exec.sh*
~/some_dir$
Yves.
|
|
|
09-13-2005, 10:35 AM
|
#5
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
Aliases, Functions, Scripts, & the Environment
All good answers, bravo.
The "pecking order" is roughly:[list=1][*]alias[*]function[*]script[/list=1]
Aliases are fast, easy, & hidden; but can't use arguments / parameters.
Functions are fast, easy, & can take arguments / parameters; but aren't hidden -- they clutter the environment.
Scripts can take arguments, do not clutter the environment -- they can be long; but they are: - Not as fast as aliases or functions.
- Must have extra lines -- e.g. '#! /bin/bash'.
- Must be in separate files.
- Must be placed in a directory in the PATH.
In my experience, Debian & MEPIS boxen are notorious for environmental function clutter.
If you want to check your box, run the following 3-part command:
Code:
env|wc ; set|grep -v "^[[:blank:]{}']\|()\|^$" |wc ; set|wc
The lines in the output are:[list=1][*]Just the environment variables.[*]All the shell variables stripped of the functions, at least as they are written on my MEPIS 3.3 box.[*]All the shell variables, including the functions.[/list=1] Mine looks like this:
Code:
$ env|wc ; set|grep -v "^[[:blank:]{}']\|()\|^$" |wc ; set|wc
32 36 952
62 76 1659
4233 14437 136980
Pretty bloated, eh.
BTW, while we are on the subject, here is one of my favorite functions:
Code:
ll ()
{
ls -l --color=always $@ | less -RS~#14
}
It takes arguments for ls & sends the output to my favorite pager, preserving the color coding & unfolding long lines.
A good example of what you can learn by R'ingTFM's, ls & less, in this case.
Last edited by archtoad6; 09-13-2005 at 10:36 AM.
|
|
|
09-13-2005, 03:33 PM
|
#6
|
Member
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232
Rep:
|
It is NOT TRUE that alias doesn't take positional parametars.
FOr example, I used to have the following alias:
Code:
alias cx='chmod u+x $1'
which I recently replaced with:
Code:
alias cx='chmod u+x $@'
That being said, I too am puzzled as to why milanche's alias doesn't work the way he/she wants it.
Last edited by frankie_DJ; 09-13-2005 at 03:38 PM.
|
|
|
09-13-2005, 04:04 PM
|
#7
|
Senior Member
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037
Rep:
|
Re: alias cs='cd $1; ls'
Quote:
Originally posted by milanche
I want to list contents of the new_dir and STAY IN IT. .... How can I fix this ....
|
try that..
Code:
alias cs="ls $1 ; cd $1"
i inverted the process but the result is what you want..
regards,
slackie1000
|
|
|
09-13-2005, 04:21 PM
|
#8
|
Member
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900
Rep:
|
Unfortunately, your example doesn't give the results he's after, slackie. It lists the current directory and then changes to the correct one.
aliases definitely do not take parameters, as stated in the man page. Your example and frankie_DJ's example work "by accident". $1 is expanded to nothing when the alias runs, so your alias expands to:
cs thisdir => ls; cd thisdir
(hopefully the bold helps illustrate why your alias appears to work, from a current-directory viewpoint)
frankie's example works the same way.
cx file => chmod u+x file
if you change frankie's alias command to this:
alias cx="chmod u+x $1;"
it will no longer work, because the filename parameter you give it will appear after the semicolon.
|
|
|
09-13-2005, 04:30 PM
|
#9
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
Thank you CroMagnon. Great counter example adding the ';' just in case they don't believe they were wrong -- it was just an accident that '$1' etc. seemed to work.
|
|
|
09-13-2005, 05:07 PM
|
#10
|
Member
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232
Rep:
|
CroMagnon, thanks for clearing that up. Quote from man pages:
"...There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below)."
|
|
|
09-13-2005, 06:53 PM
|
#11
|
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
|
I hate being contradicted, esp. when I'm right. May I take that as an apology? (One down & one to go ... )
|
|
|
09-13-2005, 06:57 PM
|
#12
|
Member
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232
Rep:
|
If you corrected me you would have gotten one.
But you went hiding, untill CroMagnon corrected me.
Still need a big daddy, huh?
|
|
|
All times are GMT -5. The time now is 07:32 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|