🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #145

Re: GNU bash shell -- how to do csh alias things in bash functions

#145
From: Chet Ramey
Date: Fri, 24 Jul 1992 20:45
69 lines
3143 bytes
In article <1992Jul24.195840.11711@news.weeg.uiowa.edu> dkeber@news.weeg.uiowa.edu (David Keber) writes:
>
>My understanding is that you cannot put command line arguments into
>bash aliases (right?), so I wrote the following two bash functions:
>
>    dir () { ls -aFglNqsT $1 | more ; }
>    ls () { ls -CFq $1 | more ; }
>
>based of the example of functions given in the man page (btw/why the
>semicolon at the end?).  THESE FUNCTIONS DO NOT WORK.  If I rename
>the ls function to foo, then they both end up working correctly!

You've just written two infinitely recursive functions.  Aren't you
proud ;-)

Functions are looked up before other commands, builtin and file system.
Each time you call `ls', it finds the function, executes the body, finds
that the first command is `ls', finds the function, ...

What you want to do is something like this:

dir() { command ls -aFglNqsT $1 | more ; }
ls() { command ls -CFq $1 | more ; }

The `command' builtin executes its arguments as a command and arguments
without including shell functions in the command search sequence.

>I want to do an
>ls with the -CFq arguements almost all the time just by typing "ls".

Another thing you can do is

alias ls="ls -CFq"

and let the rest of the tokens on the line alone.  Bash takes special
pains to avoid infinite recursion when expanding aliases.

>Another question I had was in converting the following alias:
>
>    alias whereis "find ~ -name '\!*' -print | more"
>
>the function I wrote was:
>
>    whereis() { find ~ -name $1 -print | more }

This works fine for me.  You can't enclose the `$1' in single quotes
because this has the effect of inhibiting the expansion of $1, as
single quotes are supposed to.

>is now, it just works unreliably.  In csh, this is supposed to give a
>list of all files matching a specific pattern, for example
>
>          whereis *.ps
>
>would list the full pathname of any file that ended in ".ps".

"whereis '*.ps'" works fine for me.  Without the single quotes, globbing
(pathname expansion) is performed before the function is called.  If you
have files in the current directory ending with .ps, they become the
results of the expansion and are passed to `whereis' as arguments.  If you
happen to be in a directory with no ps files, the pattern is left unchanged
and things work as you expect. 

Chet
-- 
``The use of history as therapy means the corruption of history as history.''
	-- Arthur Schlesinger

Chet Ramey, Case Western Reserve University	Internet: chet@po.CWRU.Edu
Technical Details
Message-ID: <1992Jul24.204557.3222@usenet.ins.cwru.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!usenet.ins.cwru.edu!odin!chet
References: <1992Jul24.195840.11711@news.weeg.uiowa.edu>

Show Full Headers
Complete Article Headers
Newsgroups: comp.unix.shell
Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!usenet.ins.cwru.edu!odin!chet
From: chet@odin.INS.CWRU.Edu (Chet Ramey)
Subject: Re: GNU bash shell -- how to do csh alias things in bash functions
Message-ID: <1992Jul24.204557.3222@usenet.ins.cwru.edu>
Keywords: bash csh shell alias functions GNU
Sender: news@usenet.ins.cwru.edu
Nntp-Posting-Host: odin.ins.cwru.edu
Organization: Case Western Reserve University, Cleveland, OH (USA)
References: <1992Jul24.195840.11711@news.weeg.uiowa.edu>
Date: Fri, 24 Jul 92 20:45:57 GMT
Lines:       70