3

I'm trying to update some scripts from tcsh to bash to reflect the bash preference of some users. Needless to say, I don't know csh. Can someone tell me what this line does?

alias prepend 'if (-d \!:2) if ("$\!:1" \!~ *"\!:2"*) export \!:1 "\!:2":${\!:1}'
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
b degnan
  • 672
  • 1
  • 14
  • 31
  • Apparently there's an `export` command (could be an alias) visible from tcsh/csh. That name conflicts with bash's built-in `export` command. You'll have to resolve that somehow. – Keith Thompson Apr 03 '14 at 15:31

1 Answers1

3

That probably prepends a directory onto a variable if said directory doesn't already exist in said variable.

Here's what it says, in English: if the second argument is a directory, then if the first argument interpreted as a variable does not contain the text of the second argument, then "export" the string "second argument colon contents of first argument" into the first argument.

It all depends upon what export does. In my experience, export is an alias for setenv, but that is not guaranteed.

I would suggest a test to see if this does what I think. First, echo your path. Then run prepend PATH /a/new/directory/that/exists. Then echo your path again. If you see that "/a/new/directory/that/exists" is now in your path variable, then you can be reasonably sure export is an alias for setenv.

Finally, this SO post lists strategies to implement a similar thing in other languages and shells.

Community
  • 1
  • 1
bishop
  • 37,830
  • 11
  • 104
  • 139
  • It depends on what `export` does; there's no such built-in command in csh/tcsh. – Keith Thompson Apr 03 '14 at 15:31
  • @KeithThompson: True. In the bastard `tcsh` environments I've been tortured with, `export` was an alias to `setenv`. Nonetheless, I've weakened my answer a bit. – bishop Apr 03 '14 at 15:34
  • I suggest that rather than just adding the word *probably*, you should explicitly acknowledge that it depends on what `export` does. Future readers may not read these comments. – Keith Thompson Apr 03 '14 at 15:39