2

I wanted to start an interactive bash shell like this :

bash (...some options) 1 2 3

so that in the shell session, I have $1=1, ...

I didn't find any options to achieve the effect.

I tried this, thought it might work :

bash -c bash _ 1 2 3

but it didn't.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Philippe
  • 20,025
  • 2
  • 23
  • 32

1 Answers1

4

From bash invoking bash:

-s

If this option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell or when reading input through a pipe.

$ bash -s 1 2 3
$ echo $1
1 
$ exit
$
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • One more question, when I do `bash --rcfile customized-bashrc -s 1 2 3`, in customized-bashrc, is there a way to get "$@" ? – Philippe Mar 28 '20 at 20:27
  • @Philippe — there are two possibilities: yes and no. Have you tried accessing `$#`, `"$@"`, `$1` etc from inside the customized startup file? There's a moderate chance they're available. It would be the work of moments to add `echo "$0: ($#) $*" >&2` near the top of the startup file, and then see whether your message appears and what the values are. That sort of context is one of the few times when it is sensible to use `$*` instead of `"$@"`. Of course, you could also write `echo "$0: ($#)" "$@" >&2`. – Jonathan Leffler Mar 28 '20 at 21:02
  • @JonathanLeffler, In customized-bashrc, I just put nothing else but `echo "$@"`, which echo nothing.Only in the shell itself, when I do `echo "$@"`, I get `1 2 3` – Philippe Mar 28 '20 at 21:14
  • When I put in customized-bashrc, `echo $# - "$@" - $1`, as you suggested, I get `0 - -` – Philippe Mar 28 '20 at 21:17
  • @Philippe — So that probably means the answer is "No" — the command line arguments are not available inside the customized startup file. Sorry, but short of hacking the Bash source, there isn't a lot you can do about it. You could scrutinize [Invoking Bash](https://www.gnu.org/software/bash/manual/bash.html#Invoking-Bash) but I didn't get enlightenment from it that would definitively help you—except … `bash -c 'exec bash -s "$@"' quarantine 1 2 3`—when I run that, the sub-shell has `$0` of `quarantine` and `$3` is 3 and `$@` contains `1`, `2` and `3`. Is that what you're after? _It's devious!_ – Jonathan Leffler Mar 28 '20 at 22:25
  • 1
    I started reading bash sources on this, and [positional parameters are set up](https://github.com/bminor/bash/blob/master/shell.c#L672) before [calling startup_file](https://github.com/bminor/bash/blob/master/shell.c#L694) and [bashrc](https://github.com/bminor/bash/blob/master/shell.c#L1113). But the `$@` is empty in startup file, so I guess it's somewhere else. As a workaround I see do it [like here](https://unix.stackexchange.com/questions/123103/how-to-keep-bash-running-after-command-execution) and `source` your rc files and pass args manually. – KamilCuk Mar 28 '20 at 22:29
  • To keep it simple for now, I'm using env vars, which are less clean than positional parameters. – Philippe Mar 28 '20 at 23:47