permaculture playing cards
The moose likes Linux / UNIX and the fly likes declaring the shell variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "declaring the shell variable" Watch "declaring the shell variable" New topic

declaring the shell variable

Bhopal Singh

Joined: Jul 21, 2005
Posts: 11
Hi All,

I want to write a shell script that can work with C-based [tcsh,csh etc] and non C-based [bash,ksh etc] shells.
I need to declare some shell variables in this script and different shells has different syntax to declare shell variables. So, my problem is how to declare my variable in script so that it can run in all the shells?

my is as follows

So, now the shell script can be invoked in following manners
1. chmod +x
2. ./


2. /bin/tcsh

In case a) the "sh" shell will be used to execute and it will run successfully, but if the b) method is used to run the script it will fail.

One of the solution I can think of is to test the shell used to invoke the script e.g.

if [invoke_shell_name contains either tcsh or csh] then
set abc=12
set myname=bhopal

So, my quesiton is can I determine the shell script used to execute my shell script on the fly?

I will appreciate any other approach to write a SHELL independent script.

Thanks and Regards,
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

I just stick to bash, myself, but if you "source" a shell script and that script has an explicit shell specififer, I'm pretty sure you can avoid having to test:

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: declaring the shell variable
It's not a secret anymore!