Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to repeat following operation a lot of times, hence I want to automate it.

sudo su
cd $workDir

Here $workDir is set in ~/.tcshrc. I tried writing two shells scripts. Script1 is temp.sh

sudo su

Script2 is temp.csh

cd $workDir

But it doesn't work as expected i.e. I don't see the cshell prompt (still on bash prompt, unlike in without-script approach) neither have I CDed into $workDir. Any help/pointers?

share|improve this question
“It doesn't work as expected.” So what did you expect? What did you observe? How do the two differ? – Gilles Oct 24 '12 at 0:20
@Gilles thanks for feedback. updated the question. – user13107 Oct 24 '12 at 5:29
up vote 1 down vote accepted

Use workDir="/dev/"; sudo su -c tcsh -c "cd $workDir; echo \$PWD" to change the folder during script.

Just remember to escape all $.


Why wouldn't you do so:

sudo ./tcsh.csh

And inside tcsh.csh contains:

cd $workDir  # need to set $workDir somewhere, depends on you
share|improve this answer
Thanks. Where does sudo su go? I'd like to automate typing it as well. – user13107 Oct 23 '12 at 2:35
Thanks for the update. I tried the part under 'Edit', but it's not working as expected. I have to be able to change the terminal prompt after running sudo ./tcsh.csh, which doesn't seem to happen. Also I have $workDir declared in ~/.tcshrc. Should I rather declare it in ~/.bashrc? – user13107 Oct 23 '12 at 2:50

You can doing something like this from (using here-doc) :

share|improve this answer

When you run

sudo su

this executes an interactive shell as root (sudo su), then when that shell terminates the csh script is executed. Similarly, the temp.csh script first executes an interactive tcsh, then changes its current directory just before exiting.

To execute a csh script as root, run

sudo ./temp.csh

To execute an interactive instance of tcsh as root, which I think is what you're trying to do, run

sudo tcsh

To execute that tcsh instance in a particular directory, change the directory before running sudo.

cd /path/to/directory && sudo tcsh

Note that in many installations, sudo wipes most of the environment, so don't expect environment variables set in the wrapper script to be available in the tcsh session.

You can't pass a script to execute at the beginning of a tcsh session, other than the profile files (/etc/csh.login and ~/.login for login shells, /etc/csh.cshrc and ~/.cshrc for all tcsh instances). If you want to run some initial commands, put them in ~/.cshrc (possibly inside a conditional construct). Alternatively, for commands that only execute programs, set environment variables, change the current directory and the like, you can use a wrapper script like the one below; this does not work to change the shell environment (for example, to set csh variables with set).

# commands go here
cd $workdir
setenv foo bar
exec tcsh
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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