179

I want to run a bash subshell, (1) run a few commands, (2) and then remain in that subshell to do as I please. I can do each of these individually:

  1. Run command using -c flag:

    $> bash -c "ls; pwd; <other commands...>"
    

    however, it immediately returns to the "super" shell after the commands are executed. I can also just run an interactive subshell:

  2. Start new bash process:

    $> bash
    

    and it won't exit the subshell until I say so explicitly... but I can't run any initial commands. The closest solution I've found is:

    $> bash -c "ls; pwd; <other commands>; exec bash"
    

    which works, but not the way I wanted to, as it runs the given commands in one subshell, and then opens a separate one for interaction.

I want to do this on a single line. Once I exit the subshell, I should return back to the regular "super"shell without incident. There must be a way~~

NB: What I am not asking...

  1. not asking where to get a hold of the bash man page
  2. not asking how to read initializing commands from a file... I know how to do this, it's not the solution I'm looking for
  3. not interested in using tmux or gnu screen
  4. not interested in giving context to this. I.e., the question is meant to be general, and not for any specific purpose
  5. if possible, I want to avoid using workarounds that sort of accomplish what I want, but in a "dirty" way. I just want to do this on a single line. In particular, I don't want to do something like xterm -e 'ls'
10
  • I can imagine an Expect solution, but it's hardly the one-liner you want. In what way is the exec bash solution unsuitable for you? Mar 9, 2012 at 16:21
  • 7
    The beauty of exec is that it replaces the first subshell with the second, so you're only left 1 shell below the parent. If your initialization commands set environment variables, they will exist in the exec'ed shell. Mar 9, 2012 at 19:45
  • 2
  • 8
    And the problem with exec is that you lose anything that's not passed down to subshells via the environment, such as non-exported variables, functions, aliases, ...
    – cjs
    Sep 15, 2018 at 3:49
  • 2
    +1 for adding all these clarifications in the question, it heads off a lot of unhelpful replies
    – phette23
    Jul 7, 2021 at 18:40

11 Answers 11

143

This can be easily done with temporary named pipes:

bash --init-file <(echo "ls; pwd")

Credit for this answer goes to the comment from Lie Ryan. I found this really useful, and it's less noticeable in the comments, so I thought it should be its own answer.

14
  • 21
    This presumably means that $HOME/.bashrc is not executed though. It would have to be included from the temporary named pipe.
    – Hubro
    Aug 4, 2015 at 21:47
  • 18
    To clarify, something like this: bash --init-file <(echo ". \"$HOME/.bashrc\"; ls; pwd")
    – Hubro
    Aug 4, 2015 at 22:02
  • 10
    This is so gross but it does work. I can't believe bash doesn't support this directly. Feb 24, 2017 at 16:07
  • 4
    @Gus, the . is a synonym for the source command: ss64.com/bash/source.html. Mar 12, 2017 at 3:18
  • 3
    Is there a way to make it work with user switching, such as sudo bash --init-file <(echo "ls; pwd") or sudo -iu username bash --init-file <(echo "ls; pwd")? Aug 21, 2018 at 18:52
21

Try this instead:

$> bash -c "ls;pwd;other commands;$SHELL"

$SHELL It makes the shell open in interactive mode, waiting for a close with exit.

3
  • 21
    FYI, this opens a new shell afterwards, so if any of the commands affect the current shell's state (e.g. sourcing a file) it might not work as expected Apr 8, 2017 at 14:47
  • 1
    @ThiefMaster you can easily circumvent this behavior (if unwanted) wrapping the init command with a subshell: bash -c "(ls;pwd;other commands;);$SHELL"
    – gallo
    Feb 14, 2022 at 23:57
  • 1
    Nice tip, but only partly true, @gagallo7; environment variables are inherited "down only", and since that will be a child shell, the parent will not see any env var assignments. See comments on original question for use of exec to retain env vars (though it probably loses anything your comment would preserve).
    – Justin
    Feb 23, 2022 at 18:06
19

You can do this in a roundabout way with a temp file, although it will take two lines:

echo "ls; pwd" > initfile
bash --init-file initfile
8
  • 2
    For a nice effect you can make the temp file remove itself by including rm $BASH_SOURCE in it. Mar 9, 2012 at 17:05
  • Eduardo, thank you. That's a nice solution, but... are you saying that this can't be done without having to fiddle with file I/O. There's obvious reasons why I would prefer to keep this as a self contained command, because the moment files come into the mix I'll have to start worrying about how to make random temp-files and then, as you mentioned, deleting them. It just requires so much more effort this way if I want to be rigorous. Hence the desire for a more minimalistic, elegant solution. Mar 9, 2012 at 17:20
  • 2
    @SABBATINILuca: I'm not saying anything like that. This is just a way, and mktemp does solve the temp file issue as @cjc pointed out. Bash could support reading the init commands from stdin, but as far as I can tell it doesn't. Specyfing - as init file and piping them half works, but Bash then exits (probably because it detected the pipeline). The elegant solution, IMHO, is to use exec. Mar 9, 2012 at 17:50
  • 2
    Doesn't this also override your normal bash initialisation ? @SABBATINILuca what are you trying to achieve with this why do you need to spawn a shell auto run some commands and then keep that shell open?
    – user9517
    Mar 9, 2012 at 17:58
  • 14
    This is an old question, but Bash can create temporary named pipes by using the following syntax: bash --init-file <(echo "ls; pwd").
    – Lie Ryan
    Feb 13, 2014 at 14:59
6

Why not use native subshells?

$ ( ls; pwd; exec $BASH; )
bar     foo     howdy
/tmp/hello/
bash-4.4$ 
bash-4.4$ exit
$

Enclosing commands with parentheses makes bash spawn a subprocess to run these commands, so you can, for example, alter the environment without affecting parent shell. This is basically more readable equivalent to the bash -c "ls; pwd; exec $BASH".

If that still looks verbose, there are two options. One is to have this snippet as a function:

$ run() { ( eval "$@"; exec $BASH; ) }
$ run 'ls; pwd;'
bar     foo     howdy
/tmp/hello/
bash-4.4$ exit
$ run 'ls;' 'pwd;'
bar     foo     howdy
/tmp/hello/
bash-4.4$ exit
$

Another is to make exec $BASH shorter:

$ R() { exec $BASH; }
$ ( ls; pwd; R )
bar     foo     howdy
/tmp/hello/
bash-4.4$ exit
$

I personally like R approach more, as there is no need to play with escaping strings.

1
  • 1
    I’m not sure if there are any caveeats using exec for the scenario the OP has in mind, but for me this is the far best solution proposed, because it uses plain bash commands without any string escaping issues.
    – Peter
    Oct 1, 2019 at 9:05
5

The "Expect solution" I was referring to is programming a bash shell with the Expect programming language:

#!/usr/bin/env expect
set init_commands [lindex $argv 0]
set bash_prompt {\$ $}              ;# adjust to suit your own prompt
spawn bash
expect -re $bash_prompt {send -- "$init_commands\r"}
interact
puts "exiting subshell"

You'd run that like: ./subshell.exp "ls; pwd"

2
  • I guess this would have the advantage of registering the commands in the history too, am I wrong? Also I'm curious if bashrc/profile is executed in this case?
    – muhuk
    Nov 7, 2015 at 10:49
  • Confirmed that this allow you to put commands in the history, which the other solutions don't. This is great for starting a process in a .screenrc file -- if you exit the started process, you don't close the screen window. Oct 29, 2018 at 13:56
5

What you need is to execute a startup script, then proceed with the interactive session.

The default startup script is ~/.bashrc, another script could be given with the --init-file option. If you simply pass an --init-file option, your script would replace the default, rather than augment it.

The solution is to pass, using the <(...) syntax, a temporary script that sources the default ~/.bashrc followed by any other commands:

bash --init-file <(echo ". ~/.bashrc; ls; pwd; ### other commands... ###")
1

If sudo -E bash does not work, I use the following, which has met my expectations so far:

sudo HOME=$HOME bash --rcfile $HOME/.bashrc

I set HOME=$HOME because I want my new session to have HOME set to my user's HOME, rather than root's HOME, which happens by default on some systems.

1

I don't have enough reputation here to comment yet, but I think the various solutions with bash's --rcfile (aka --init-file) are the most practical.

For a POSIX-compatible solution, you could also try something like

sh -si < my_init_file

where my_init_file terminates with

exec </dev/tty

which connects stdin to the terminal.

You could even try

sh -i -c'my startup commands; exec </dev/tty'

Note that making the shell interactive (with -i) means that certain errors are treated more leniently.

0

less elegant than --init-file, but perhaps more instrumentable:

fn(){
    echo 'hello from exported function'
}

while read -a commands
do
    eval ${commands[@]}
done
0

I accomplish basically the same thing by just using a script, typically for the purpose of setting environment variables for use in a specific project directory;

$ cat shell.sh
#!/bin/bash
export PATH=$PWD/bin:$PATH
export USERNAME=foo
export PASSWORD=bar
export DB_SERVER=http://localhost:6001
bash

$ echo ${USERNAME:-none}
none

$ ./shell.sh

$ echo $USERNAME
foo

This drops you into an interactive bash shell after all the environment adjustments are made; you can update this script with the relevant other commands you want to run.

0
$ bash --init-file <(echo 'ls; pwd')
$ bash --rcfile <(echo 'ls; pwd')

In case you can't use process substitution:

$ cat script
ls; pwd
$ bash --init-file script

With sh (dash, busybox):

$ ENV=script sh

Or:

$ bash -c 'ls; pwd; exec bash'
$ sh -c 'ls; pwd; exec sh'

You must log in to answer this question.

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