Shell scripting shell inside shell Shell scripting shell inside shell shell shell

Shell scripting shell inside shell


If you want to execute only one single command, you can use the -c option

csh -c 'echo $SHELL'ksh -c 'echo $SHELL'

If you want to execute several commands, or even a whole script in a child-shell, you can use the here-document feature of bash and use the -s (read commands from stdin) on the child shells:

#!/bin/bashecho "this is bash"csh -s <<- EOF    echo "here go the commands for csh"    echo "and another one..."EOFecho "this is bash again"ksh -s <<- EOF    echo "and now, we're in ksh"EOF

Note that you can't easily check the shell you are in by echo $SHELL, because the parent shell expands this variable to the text /././bash. If you want to be sure that the child shell works, you should check if a shell-specific syntax is working or not.


It is possible to use the command line options provided by each shell to run a snippet of code.

For example, for bash use the -c option:

bash -c $codebash -c 'echo hello'

zsh and fish also use the -c option.

Other shells will state the options they use in their man pages.


You need to use the -c command line option if you want to pass commands on bash startup:

#!/bin/bash# We are in bash already ...echo $SHELLcsh -c 'echo $SHELL'ksh -c 'echo $SHELL'