3

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.

closed as off-topic by jasonwryan, Sparhawk, Jeff Schaller, slm Jul 16 '16 at 22:50

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – jasonwryan, Sparhawk, Jeff Schaller, slm
If this question can be reworded to fit the rules in the help center, please edit the question.

  • @Fox Holy crap I can't believe I never realized the spaces were in all the examples. Thanks a ton! – MozerShmozer Jul 16 '16 at 20:16
7

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.

  • 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 kornshell.com/doc/faq.html – Stephen Harris Jul 16 '16 at 23:33
0

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
}

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