Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after running program leave interactive shell to use

Tags:

bash

shell

zsh

exec

I want to run any program given as argument, through shell then want that shell left as interactive shell to use later.

#!/bin/bash
bash -i <<EOF
$@
exec <> /dev/tty
EOF

But it is not working with zsh

#!/bin/bash
zsh -i <<EOF
$@
exec <> /dev/tty
EOF

as well as if somebody know more improved way to do it please let me know.

like image 605
Sharad Avatar asked Jun 02 '11 18:06

Sharad


1 Answers

Approach 1: bash, zsh and a few other shells read a file whose name is in the ENV environment variable after the usual rc files and before the interactive commands or the script to run. However bash only does this if invoked as sh, and zsh only does this if invoked as sh or ksh, which is rather limiting.

temp_rc=$(mktemp)
cat <<'EOF' >"$temp_rc"
mycommand --option
rm -- "$0"
EOF
ENV=$temp_rc sh

Approach 2: make the shell read a different rc file, which sources the usual rc file and contains a call to the program you want to run. For example, for bash:

temp_rc=$(mktemp)
cat <<'EOF' >"$temp_rc"
mycommand --option
if [ -e ~/.bashrc ]; then . ~/.bashrc; fi
rm -- "$0"
EOF
bash --rcfile "$temp_rc"

For zsh, the file has to be called .zshrc, you can only specify a different directory.

temp_dir=$(mktemp -d)
cat <<'EOF' >"$temp_dir/.zshrc"
mycommand --option
if [ -e ~/.zshrc ]; then . ~/.zshrc; fi
rm -- $0; rmdir ${0:h}
EOF
ZDOTDIR=$temp_dir zsh
like image 171
Gilles 'SO- stop being evil' Avatar answered Sep 21 '22 04:09

Gilles 'SO- stop being evil'