Converting an old .cshrc file into a .bash_aliases file.  I've run into a few syntax issues regarding the conversion, but this involves a little more in depth knowledge of Bash.In [t]Csh, the shell would hang when opening xwindow applications, unless they were opened in the background using an ampersand (e.g., firefox &).  To avoid this, I would create certain aliases in Csh to run the task in the background, but still leave the command line open for options (using \!*):alias ff 'firefox \!* &When this is converted to Bash it'll do something like this:ff () { command ff $@ & }I still am not sure when to use a function and when to use alias, but I came into a predicament when trying to wrap more options around an already defined alias:win () { command xterm -geometry 105x25 -font 8x13 -bc $@ & }blue () { command win -bg gray33 -fg gray39 $@ ; }If I remove the command within blue (e.g., blue () { win -bg ...) it works as expected.So I'm curious about two things:When to use alias vs function definitions?When to use command; if I did alias win='xterm ...' would I have to do command win then?
Wrapping aliases in Bash
bash;alias;csh
One, use functions for anything non-trivial; a trivial alias would be to set some flag by default:# no license spamalias gdb='gdb -q'for pretty much anything more complicated I use a function.Two, command refers to a command, such as /bin/ls or the like; aliases are not commands, nor are functions commands. You likely have no win command, so command win will fail to call the function, as that is not a command.Infinite loops are really easy to create, by the wayalias fooalias=foofuncfunction foofunc() { fooalias; }which is why if one is writing a function with the same name as a command then command becomes really handy so the shell doesn't attempt to call the function forever:# loops until stack death or segfault or somethingfunction xterm() { xterm ... }# goodfunction xterm() { command xterm ... }If you're just trying to get some other function to call something else that may be a command or function, just call it as is. The one that does need to call the command must use command.function winwinwin () { winwin ... }function winwin () { win ... }function win () { xterm ... }function xterm () { command xterm ... }winwinwin would call the winwin function would call the win function would call the xterm function and then that would instead call the named command.On the function front, that is optional, but I use it as it makes searching for functions rather easy (fgrep function ...) than having to look for things followed by maybe whitespace and err umm do parens need to be escaped in the regex engine I'm using? For the priority, it's easy to make tests or print notices that show what is happening:ls () { ls -k; echo funcall; }alias ls='echo blat'lsIn general it's probably a better idea to define functions first, and aliases after those are setup, for least surprise:bash-4.1$ alias ls='echo blat'bash-4.1$ ls () { command ls -k; echo funcall; }bash: syntax error near unexpected token `('bash-4.1$ function ls () { command ls -k; echo funcall; }bash-4.1$ 