3

Please note: Although this question is essentially the same as this linked question, I did not mark it as "solving my problem" because the answers provided there are not very satisfactory. The answer I accepted below is cleaner and simpler. Take a look!

I know how to pass bash a custom command to execute, e.g. like this:

bash -c "/bin/echo hello"

When I do this, bash executes the argument of -c and then exits. How can I get it to give me an interactive prompt when I'm done? Adding -i has no effect. I don't want to write the custom commands into a file that .bashrc always reads.

I've been making do with bash -c "commands; exec bash -i", but it's not ideal since it starts bash twice. Is there a way to get the same effect while starting bash only once?

3
  • 1
    Well, the question is admittedly the same, but I didn't like any of the answers given there. I just accepted a far better answer. Not sure what the recommended course of action is for this type of duplication...
    – alexis
    Commented Nov 15, 2016 at 21:17
  • I agree that the accepted answer is a better answer than those on the duplicate question. Commented Nov 16, 2016 at 16:57
  • 1
    Update: I asked for guidance about this situation on meta. It turns out that if you like the answer(s) here better than those in the original question, it's possible to mark the older question as a duplicate of the newer one. I see there are some votes here to reopen, so I'll leave it up to you all. I'm just glad the question was open long enough for @Paul's great answer.
    – alexis
    Commented Nov 16, 2016 at 20:11

1 Answer 1

6

You can use the --init-file option like this bash --init-file <(echo "echo hello"). This will not source your .bashrc file at all. If you want the .bashrc file sourced you can add that like this bash --init-file <(echo ". ~/.bashrc; echo hello"). Or you can put all the commands you want to execute in a file and use bash --init-file file_with_commands

2
  • 1
    I like this! Much cleaner than redirecting from /dev/tty as in the linked question. Yes, I need to run .bashrc to have a fully functional interactive bash. Gotta try out your idea a bit...
    – alexis
    Commented Nov 15, 2016 at 20:54
  • Works like a charm, thanks! You should add it to this question too, which asks the same thing.
    – alexis
    Commented Nov 15, 2016 at 21:18

Not the answer you're looking for? Browse other questions tagged .