Linux - NewbieThis 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.
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.
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.
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:
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:
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.
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.
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.
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)."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.