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

2
  • 1
    Are you sure a symlink wouldn't be the better solution?
    – Daniel Beck
    Jan 8, 2011 at 19:35
  • Hello Daniel, in this case no. Jan 8, 2011 at 19:37

2 Answers 2

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.

1
  • 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) Jan 8, 2011 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.

7
  • Hello Neurolysis, can you please explain? Jan 8, 2011 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: tldp.org/LDP/abs/html/functions.html Jan 8, 2011 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
    – heavyd
    Jan 8, 2011 at 19:46
  • ok thanks the last portion to this is a post from: 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 Jan 8, 2011 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. Jan 8, 2011 at 19:48

You must log in to answer this question.

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