0

I am trying to set an alias that would allow me to quickly cd to the following directories:

/home/user/asreera/Aravind/sample/src
/home/user/asreera/Aravind/sample2/src

First I set the variable:

set use=/home/user/asreera/Aravind/

Then I've set alias:

alias mov 'cd $use/*/src'

My intended usage is:

mov sample

It should go to directory /home/user/asreera/Aravind/sample/src.

In case of mov sample2 it should go to directory /home/user/asreera/Aravind/sample2/src

When I use this alias I'm getting an error cd:too many arguments

Could someone please tell me what I'm doing wrong in this?

3
  • 1
    Wild cards are not magical and there is no way this can be done automatically without much clearer specs. Once you figure out exactly how it could be done logically, write it in a shell script and make use that instead of an alias. Commented May 30, 2016 at 7:16
  • 3
    * does not do what you think it does. You can use a shell function: $1 would then go where you put the *. Commented May 30, 2016 at 7:37
  • @richard How do you define a function in tcsh?
    – techraf
    Commented May 30, 2016 at 8:03

1 Answer 1

2

You should use:

alias mov 'cd "$use"/\!*/src'

!* in csh/tcsh alias resolves to all arguments of the command being run (i.e. what you put after mov).

See Writing Aliases in csh and tcsh for other options:

  • !! is the whole command line
  • !* is all the arguments of the command
  • !:1 is the first argument of the command
  • !:2 is the second argument of the command
  • !$ is the last argument of the command

Each must be escaped by \.


Could someone please tell me what I'm doing wrong in this?

When you execute:

mov sample

Shell resolves the alias to:

cd $use/*/src sample

Then expands the variable and executes:

cd /home/user/asreera/Aravind//*/src sample

cd command complains it has too many arguments.

5
  • @techarf Could you please explain importance of "!" here? what exactly it is doing
    – Aravind
    Commented May 30, 2016 at 8:34
  • !* in csh alias resolves to all arguments of the command being run (i.e. what you put after mov). See this for other options.
    – techraf
    Commented May 30, 2016 at 8:38
  • alias mov 'cd "$use"/\!:1/src' might make more sense — it doesn't make sense for this alias to be called with more than one argument. P.S. You should always quote your shell variable references (e.g., "$use") unless you have a good reason not to, and you’re sure you know what you’re doing. Commented May 30, 2016 at 10:06
  • No it doesn't make sense, imho, for this very reason. If you call the alias with more than one argument it is better to fail than take its first argument and pretend everything was ok.
    – techraf
    Commented May 30, 2016 at 10:08
  • @techraf can I use this same concept in ksh
    – Aravind
    Commented Feb 13, 2018 at 12:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .