How to start bash and immediately "type" command into it?

2

I want bash to first execute a command, but after the command finishes (or Ctrl+C'ed) go into interactive mode (preferrably with the command in history, available for up).

How to attain it in easiest and the most beautiful way?

Inspired by cmd.exe's /K:

$ wineconsole cmd.exe /K echo qqq
qqq

Z:\home\wine>

Vi.

Posted 2013-02-04T21:42:52.203

Reputation: 13 705

Answers

3

Cleaner version of the Scott's answer:

  1. Put this to .bash_profile:

    if [ ! -z "$EXECUTE_COMMAND" ]; then
        history -s "$EXECUTE_COMMAND"
        $EXECUTE_COMMAND
    fi
    
  2. Start bash this way:

    $ EXECUTE_COMMAND='ping 127.0.0.1' bash -l
    PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
    64 bytes from 127.0.0.1: icmp_req=1 ttl=64 time=0.177 ms
    ...
    ^C
    5 packets transmitted, 5 received, 0% packet loss, time 3996ms
    $
    $ ping 127.0.0.1
    

Vi.

Posted 2013-02-04T21:42:52.203

Reputation: 13 705

Good cleanup.  I’m glad to see that history –s works in .bash_profile. – Scott – 2013-02-06T16:31:02.547

2

I’m not 100% sure that I understand exactly what you’re asking for, and (alas) I don’t have access to a system with bash on it at the moment, but maybe the following will help you.  Put this at the end of your .bashrc:

if [ -f ~/.bash_initial_command ]
then
    . ~/.bash_initial_command
    rm ~/.bash_initial_command
fi

(where .bash_initial_command is an arbitrary filename; pick a different one if you want), and then write a script that does this:

(echo "history –s '$@'"; echo "$@") > ~/.bash_initial_command

I.e., this puts the command (that is presented to the above script as argument(s)) in a place where the next interactive bash to start will find it and execute it.  (E.g., if you run set_up_cmd ls –l, then your next bash will run ls –l.)  history –s puts a command into history without executing it.
Note: I do not know whether this will work from .bashrc.

This will need more refinement if you will want to use commands with quotes (i.e., quoted special characters) or redirection (< or >); or compound commands (i.e., containing ;, |, &&, or ||).

A minor problem: if you Ctrl+C the command, that will probably terminate the .bashrc processing and thereby prevent the .bash_initial_command file from being deleted.  You can probably handle that by doing

if [ -f ~/.bash_initial_command ]
then
    mv ~/.bash_initial_command ~/.bash_initial_command.$$
    trap "rm -f ~/.bash_initial_command.$$" 0
    . ~/.bash_initial_command.$$
    rm -f ~/.bash_initial_command.$$
fi

This gives .bash_initial_command a unique name, so as to avoid collisions, and arranges that it will be deleted when the shell exits.

If you need to be able to fire up several of these simultaneously, consider building the “tty” name $(basename $(tty)) into the .bash_initial_command filename.

Of course, doing this makes it all the more important to be sure that your home directory is not writable by anybody but yourself.

Scott

Posted 2013-02-04T21:42:52.203

Reputation: 17 653

A bit quirky... – Vi. – 2013-02-05T21:07:01.670

@Vi.: You don’t want to know how sausage is made, either.  But does it do what you want? – Scott – 2013-02-05T21:20:48.080

Now looking how to make it as more or less simple script without .bash_profile hackery. – Vi. – 2013-02-05T21:24:03.677

Done (without `tty` or .bash_initial_command, although still with .bash_profile) – Vi. – 2013-02-05T21:46:15.457

0

You can add the command to your .bash_profile or .bashrc. It will run as soon as you open a new window or login to your machine.

See this page on the differences between the two files.

Stephen Schrauger

Posted 2013-02-04T21:42:52.203

Reputation: 504

Or do you mean you want to start a new bash process from a command line that is already started, pass in the command as a parameter, then stay within that bash process after the command is complete? – Stephen Schrauger – 2013-02-04T21:56:30.537

Yes. Actually I want to start gnome-terminal with several tabs pre-opened, but where I can Ctrl+C and restart (probaby with added parmeters) commands easily. – Vi. – 2013-02-04T23:43:10.163