4

Forgive me; I'm pretty new to bash files and the like. Here is a copy of my .bashrc:

alias k='kate 2>/dev/null 1>&2 & disown'

function kk {kate 2>/dev/null 1>&2 & disown}

The alias in the first line works fine, but the second line throws:

bash: /home/mozershmozer/.bashrc: line 3: syntax error near unexpected token `{kate'
bash: /home/mozershmozer/.bashrc: line 3: `function kk {kate 2>/dev/null >1>&2 & disown}'

I'm running Linux Mint 17.3 and those are the only two lines in my .bashrc file. Pretty much everything else on my machine is default vanilla. Ultimately I want to play around with the function to get it to do some specific things, but I hit the syntax wall immediately. The exact function I have listed here is just a sort of experimental dummy to allow me to learn the syntax more clearly.

2 Answers2

10

In bash and other POSIX shells, { and } aren't exactly special symbols so much as they are special words in this context. When creating a compound command like in your function definition, it is important that they remain words, i.e. surrounded by whitespace.

The final command in a single-line function definition like this must be terminated by a semicolon. Otherwise the closing brace } is treated as an argument to the command.


As an aside, if you want your function to be portable to other POSIX shells, it is better to use a different function syntax:

kk () { kate 2>/dev/null 1>&2 & disown; }

The use of function is specific to bash, while the form given here works with bash as well as others like sh, Korn and Almquist shells.

disown is also bash specific.

Fox
  • 8,193
  • disown is also bash-specific, but the POSIX-compatible function syntax is preferred when possible. – Fox Jul 16 '16 at 21:03
  • I do not get the 2nd bit in your comment. Is there a posix-compatible syntax for disown? what is it? Could you put the info from your comment in your answer. – ctrl-alt-delor Jul 16 '16 at 21:39
  • @richard Oh, thanks for the edit. nohup is sort of but not really a POSIX-compatible disown, but it works differently and is outside the scope of this question. – Fox Jul 16 '16 at 21:48
  • 1
    FWIW, function is also in ksh93 (it may have started there; I'm not sure). ksh88 only had the f() form. In ksh there are differences between function f and f() definitions (eg typeset scoping for local variables). – Stephen Harris Jul 16 '16 at 23:23
  • Correction, ksh88 also had function f format, but the meaning changed for f() between ksh88 and ksh93 to become POSIX compliant. See Q18 of http://kornshell.com/doc/faq.html – Stephen Harris Jul 16 '16 at 23:33
  • @StephenHarris, actually ksh's function f { ...; } predates the f() cmd SysV sh syntax (long before ksh88, IIRC it's 1981 vs 1982 though ksh wasn't released outside of AT&T before 1983) – Stéphane Chazelas Jan 11 '21 at 15:16
  • disown is not bash-specific. It comes from zsh (1.0 1990), added much later to bash (2.0, 1996) – Stéphane Chazelas Jan 11 '21 at 15:22
2

Putting it on to multiple lines avoids having to put in the extra ;, and gives one way to write a function, even when it contains lots of commands.

function kk {
    kate 2>/dev/null 1>&2 & disown
}