10

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.

4 Answers 4

4

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
7
  • Neither bash nor zsh on my system accept --rcfile=... option. Bash is 4.1.9(2), zsh is 4.3.11. Bash accepts only --rcfile ... (without =), zsh accepts --rcs ... (again without = and with different name). You don't have to name rcs file .zshrc: for me zsh --rcs <(echo "echo abc") worked well despite the fact that <(...) generates name like /proc/self/fd/{N}.
    – ZyX
    Jun 2, 2011 at 21:08
  • @ZyX: Now how did I manage to get both wrong? You're right about bash (it doesn't respect GNU conventions!). Zsh's --rcs only tells it to read the usual files, which it does by default (--norcs is the useful option); when you ran it, you just executed a script, without dropping into interactive mode afterwards. Jun 2, 2011 at 21:13
  • Missed this fact. I somehow managed to get additional zsh in outputs of pstree -h with --rcs option and thus thought it is a replacement for --rcfile. Cannot reproduce this now, guess it is because I launched something like zsh -h in order to get help before (zsh -h sets histignoredups).
    – ZyX
    Jun 2, 2011 at 21:27
  • 1
    By the way, here is fourth approach: use screen and screen -S launchedSession -X stuff $'mycommand --option\n'.
    – ZyX
    Jun 2, 2011 at 21:34
  • Seems it is necessary to use rm -- "$temp_rc" Also had to use bash --init-file, not bash --rcfile Feb 21, 2013 at 20:12
2

Why don't you simply start a new shell for interactive input?

#!/bin/sh
$@
exec zsh
0

I use this in a script to call gui programs from the shell, haven't tested it with zsh though

nohup $@ >/dev/null 2>/dev/null &
1
  • 1
    That will work in zsh too, but an equivalent shortcut syntax would be $@ &>/dev/null &! to redirect both stdout and stderr and execute in the background with nohup.
    – Caleb
    Jun 3, 2011 at 8:13
0
$ cat ~/bin/ish
#!/bin/zsh
bash -i <<EOF
$@ < /dev/tty
exec <> /dev/tty
EOF

$
$
$ ~/bin/ish vim
stty: standard input: Inappropriate ioctl for device

At this point vim is opened.

$ vim < /dev/tty
$ exec <> /dev/tty
$ 
$

shell is left for you to do other work. In my question the bash shell's STDIN was HEREDOC (<< EOF) so for so it not working for command who want to read from TTY. But after providing the command input from /dev/tty it start working.

I am not able to find how to correct warning

stty: standard input: Inappropriate ioctl for device

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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