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.