1

I am trying to create an alias in bash for example:

alias cat /home/file='cat /home/thisfile'

I have no idea why this is not working. I have tried everywhere to find the answer on google and from what I can tell this website will get the answer. -Janice

Janice Young
  • 21
  • 1
  • 2

2 Answers2

5

You can use aliases only for commands, not for their arguments.

If you want /home/file to be replaced by /home/thisfile, but only if it's the first argument of the cat command, you can define a cat function that tests its argument and calls the underlying command appropriately:

cat () {
  if [ "$1" = "/home/file" ]; then shift; set "/home/thisfile" "$@"; fi
  command cat "$@"
}

But I doubt that's what you really want, it would be a strange requirement. Daniel Beck's suggestion of a symbolic sounds right. Since you reject it, you should explain more of what you're trying to accomplish. Maybe then people can offer better suggestions.

Gilles 'SO- stop being evil'
  • 69,786
  • 21
  • 137
  • 178
  • Gilles, your command worked perfectly! I guess i better study up on function calls, etc. You, the other users, and this website are lifesavers. Thanks ~ =0) – Janice Young Jan 08 '11 at 20:33
2

You can't have an alias that contains two words, use a function instead and use awk to separate the captures.

Matthieu Cartier
  • 3,500
  • 25
  • 36
  • Hello Neurolysis, can you please explain? – Janice Young Jan 08 '11 at 19:41
  • My regex is pretty bad, so I'd rather not post an example that specifically relates to this case (because in all likelihood I'll get it wrong). There is some pretty good documentation to get you started though: http://tldp.org/LDP/abs/html/functions.html – Matthieu Cartier Jan 08 '11 at 19:46
  • 1
    @Janicem it looks like you are trying to make an alias for `cat /home/file`, aliases can only be a single word, no spaces, see [this question](http://superuser.com/q/105375/820) – heavyd Jan 08 '11 at 19:46
  • ok thanks the last portion to this is a post from: http://efreedom.com/Question/3-105375/Bash-Spaces-Alias-Name is there any way to incorporate it like that? or am I way off course?. Thank you. Janice – Janice Young Jan 08 '11 at 19:47
  • Oh yes, if you're asking about aliases, aliases can only be a single word. I assumed you were asking about functions. – Matthieu Cartier Jan 08 '11 at 19:48
  • It's more complex than that, because you want to capture from *within* the argument, which probably will require regex and awk. – Matthieu Cartier Jan 08 '11 at 19:49
  • I would like to thank you all. I am absolutely stunned at how fast and awesome this site is and its users. I have found out more info here in the last day looking around than in weeks searching Google and its links for specific stuff. – Janice Young Jan 08 '11 at 19:55