We help IT Professionals succeed at work.

bash };:

ahoffmann
ahoffmann asked
on
Medium Priority
719 Views
Last Modified: 2010-04-21
I'm asking in this topic 'cause I think most bash experts can be found here;-)

Here we go: can somebody please explain how the 2'nd and 3'rd : are interpreted by bash in following command.

     :() {;:};:

I assume bash uses them as modifiers.

NOTE, this command is dangerous, take care when using.
Anyway, my question is not just a joke, 'cause I'm using scripts like this:
     # ..
     debug=echo     # set to : to switch debug off
     # ..
     $debug some command and args
            some command and args
     # ..
Comment
Watch Question

Commented:
Commented:
SHELL BUILTIN COMMANDS
       : [arguments]
              No effect; the command does nothing beyond  expand-
              ing arguments and performing any specified redirec-
              tions.  A zero exit code is returned.
and

       { list; }
              list is simply executed in the current shell  envi-
              ronment.   This  is  known as a group command.  The
              return status is the exit status of list.

: is a noop comman. In due processof expanding arguments.

In your case however, you overwrite the default ':' command by appending (). Which is the way to create a function. Thus ':() { echo hello }'  will result when executed ':' to "hello". Unfortunately, for this to work, you need a command between { and ;.

':() { echo hello ; }' will result in 'hello'.

Adding : before closing } will result in a recursive call of the (yet unfinished) definition until you run out of stack space (Segmentation fault). The third : would then run your new command.

As it happends, it doesn't compile.

To get closer to do what you want, try

$ dump() { : }
$ debug="dump"
$ $debug blah, jadeha
$

However, that still doesn't protect you from expansion using backticks expansion (`) nor from errors during expansion of ':' or even ';' .. so it's not wise to do that either.

Better put your debug messages in single quotes ('  ') in the first place.  

It really depends on what you want to achieve

K.

CERTIFIED EXPERT

Author

Commented:
> In your case however, you overwrite the default ':' command by appending (). Which is the way to create a function.

Hmm, does this mena that bash's parser does not require the function keyword to define a new function, **and** that a function name may contain punctation characters.
Or is this a result of a lazy grammer definition for the parser?

Anyway, if so, I know know why I avoided using bash since the beginning of ages ...

Commented:
Hi
  in bash, the function command is optional. You can define functions with or without it. I don't think that it was intended that you can replace the : command. Obviously, you can in the version I tried.

The function name _should_ be only alphanumerical characters and '_'.

K.
CERTIFIED EXPERT

Author

Commented:
kyrmit, this was a perfect answer, even the solution (bash) does not satisfy my requirements.
Enjoy the increased points ;-)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.