To give more details, the situation is this.

I'm trying to prepare modulefiles (i.e., of the type used by the environment-modules package) to set up environment and aliases for third-party software. Some of the aliases, in particular, are giving me grief. The shell resource files provided by the author -- which I'm hoping to get away from and towards a shell-agnostic environment, hence my interest in modulefiles -- set aliases that incorporate literal curly braces. When I try to put the same string in the modulefile, the contents of the curly braces are expanded.

It seems that in csh/tcsh, the set-alias command of the modulefile is processed and turned into the "alias" command as offered by the shell. This is then processed using eval, as follows:

eval `alias foo 'thing to alias foo to';`

(yes, the trailing semicolon is part of the command).

I can't get away from any of those elements -- the eval, the lack of double quotes around the evaluated string, or the single quotes around the alias text -- without compiling a custom version of modules, which I'm loath to do.

Now, if the alias contains curly braces, I can't see how, in the context of an eval statement, to protect them so they aren't interpreted. I've tried backslashes from here to Timbuktu, I've tried double quotes, I've tried single quotes, I've tried single quotes inside double quotes, I've tried combinations of the above. So far, nothing's worked, and it's bordering on the absurd. Despite extensive Googling, I haven't been able to unearth any answers.

Is there a way to escape the curly braces? If not, let me know, and I'll get on with my life; if so, and it doesn't involve any recompilation, I'd much appreciate any advice. Thanks!

| |
  • I'm throwing a guess out there, but what about escaped unicode for the curly brace? U+007B and U+007D refer. In fact, there's an SO question that relates: stackoverflow.com/a/5740875/114600 – user3463 Apr 27 '12 at 3:57
  • Thanks, Randolph, but no luck. If I put the unicode characters in the modulefile, they get "construed" long before they reach the eval command. It looks like the modulefile parser, written in TCL, does that. I found the same thing with the hex strings \x7B and \x7D yesterday. – Valtandor Apr 27 '12 at 4:16

My solution for tcsh/csh is a little verbose. It is to stick "{" into a separate variable.

  set lc="{"
  set rc="}"

I do the same for a dollar sign, which I have trouble escaping. Highly useful for grep regular expressions.

Here's my cut/paste from my .cshrc:

  set dollar = \$

The usage result, like I said, is verbose, but at least works:

> echo "${dollar}${lc}this is inside curlybraces${rc}"
${this is inside curlybraces}

Note that you need those extra quote marks.

Hope this helps!

| |

Another solution:

set-alias foo {env LD_LIBRARY_PATH=`printf "\044\173LD_LIBRARY_PATH\175"`:/some/path command-to-run}

The 044, 173, and 175 are octal code for ascii characters $, {, and }

| |

Use 2 single quotes for protecting (escaping) curly braces. You may need one double quote (") when you echo the variable:

set a=''{''
echo "$a"

Also, echo within back-quotes () can be used with the escaped brackets:

set b = " COMMANDS `echo ''{''` MORE COMMANDS `echo ''}''` "  
echo "$b"
| |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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