Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Badly placed ()'s while declaring a function in tcsh

Tags:

linux

shell

I'm trying to declare a function in tcsh and to call it.

#! /bin/tcsh -f

helloWorld () {
    echo "a"
}

helloWorld

I'm getting the following error:

< 512 mews2895 ~/tmp/script> 1.sh
Badly placed ()'s.

Does anyone here what the problem might be?

Thanks

like image 682
Noam Mizrachi Avatar asked Sep 20 '25 21:09

Noam Mizrachi


2 Answers

tcsh does not support functions.

Best solution: Use a shell that does, such as bash.

If you must use tcsh for some reason, aliases will solve your immediate problem, but are much weaker than functions.

alias helloWorld 'echo "a"'

Another possible solution is to invoke a separate script. (You'll have to ensure that the invoked script is in your $PATH.)

like image 134
Keith Thompson Avatar answered Sep 22 '25 10:09

Keith Thompson


There are not functions in tcsh. So I see 2 options:

  • Use aliases: https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.2.0/com.ibm.zos.v2r2.bpxa500/alias.htm

  • Use goto. (People tend to criticize go-to, but It actually depends on the context).

There is an other option, use source if you want to organize your code with multiple files:

To run a shell script in your current environment, without creating a new process, use the source command. You could run the calculate shell script this way: source calculate Should you want to use a shell script that updates a variable in the current environment, run it with the source command.

src: OS/390 UNIX System Services tcsh (C Shell) Kit Support Guide - IBM

I think that 'use a different shell' should not be a valid response.

Regards, Pablo

like image 43
pablomtz Avatar answered Sep 22 '25 11:09

pablomtz