LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 09-12-2005, 11:53 PM   #1
milanche
LQ Newbie
 
Registered: Sep 2005
Distribution: Fedora
Posts: 1

Rep: Reputation: 0
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.
 
Old 09-13-2005, 02:17 AM   #2
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,604

Rep: Reputation: 47
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
 
Old 09-13-2005, 05:20 AM   #3
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
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
 
Old 09-13-2005, 06:34 AM   #4
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
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.
 
Old 09-13-2005, 09:35 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
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 09:36 AM.
 
Old 09-13-2005, 02:33 PM   #6
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
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 02:38 PM.
 
Old 09-13-2005, 03:04 PM   #7
slackie1000
Senior Member
 
Registered: Dec 2003
Location: Brasil
Distribution: Arch
Posts: 1,037

Rep: Reputation: 45
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
 
Old 09-13-2005, 03:21 PM   #8
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
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.
 
Old 09-13-2005, 03:30 PM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
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.
 
Old 09-13-2005, 04:07 PM   #10
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
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)."
 
Old 09-13-2005, 05:53 PM   #11
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
I hate being contradicted, esp. when I'm right. May I take that as an apology? (One down & one to go ... )
 
Old 09-13-2005, 05:57 PM   #12
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
If you corrected me you would have gotten one.

But you went hiding, untill CroMagnon corrected me.

Still need a big daddy, huh?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to paginate in the 'LS' command? jhecht Linux - Software 10 04-13-2010 06:15 AM
Alias question (2 commands for one alias) gflores Linux - Newbie 3 01-21-2006 12:40 AM
How to make alias of 'ls' in winxp/dos servnov General 4 11-04-2004 08:39 PM
'ls' command... sramelyk Slackware 7 10-01-2003 12:45 PM
general 'ls' question purpleburple Linux - General 8 04-17-2003 12:21 PM


All times are GMT -5. The time now is 08:31 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration