Now I try to create an alias for this command:

alias cdport    'cd `whereis \!^ | awk \'{print $2}\'`'

Can't find any good examples of using more than 2 commands in a sequence.

How to fix that code? Thanks.

  • Your general syntax looks correct. But you need to remove the filename from $2 to get a directory that you can cd to. – Barmar Nov 17 '14 at 21:56
  • @Barmar it returns "Unmatched `." after "source .cshrc" – kAldown Nov 17 '14 at 23:27

You can't escape quotes in strings in csh. You need to switch to double-quoted strings to enter those characters:

alias cdport 'cd `whereis \!^ | awk '"'"'{sub("/[^/]*$", "/", $2); print $2}'"'"'`'

I've also fixed the awk script to remove the filename from the output of whereis, so you just get the directory part.

| improve this answer | |
  • Actually I put awk only for removing filename. I thought that print $2 (second column) from first command output is right scenario. – kAldown Nov 18 '14 at 11:55
  • On my Debian system, the output of whereis find looks like find: /usr/bin/find /usr/share/man/man1/find.1.gz, so $2 would be /usr/bin/find. cd needs a directory, so it should be cd /usr/bin. – Barmar Nov 18 '14 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.