LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 06-08-2022, 05:01 AM   #1
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,834
Blog Entries: 17

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Different ways to invoke and execute bash


You're all pretty clever people here, so I can't think of a better place to ask this actually. It might seem like a weird question, but please bare with me.
So, I'm looking into different ways to execute and invoke bash from bash, and I'm hoping some of you guys can add a method or two here that I can study further. The purpose mainly is in regards to different exclusive executions of bash, in the same shell, but in particular in subshells and subshells of subshell etc.

Now, I've come up with a few different ways to do it myself, about 10 that has widely different implications in terms of execution path and context. And yes, executing bash through a script/file is also included as an option (and each different file would be two distinct ways actually).

Which would mean that executing bash like below, are all different ways (despite all except the last, in a way, is the same):
/bin/bash
bash
sh bash
sh /bin/bash
sh bashexecute.script (content: /bin/bash)

Or something like (sudo) su and (sudo) su - in regards to subshells..

Anyways, not too complicated please, but if anyone have some other ways to add here, it would be greatly appreciated. And of particular interest is descending into various subshells. I also don't mind if you even have some tips in regards to agetty and login in the same context.

Thanks!

Last edited by zeebra; 06-08-2022 at 06:24 AM.
 
Old 06-08-2022, 05:12 AM   #2
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,834

Original Poster
Blog Entries: 17

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Let's assume for the sake of it/rules that the starting point is agetty/login/bash in tty1 and we don't have a GUI (but we do have /dev/fb0).
That's unless you have some additional tips in regards to agetty or login.

Ok, I realize that you might know a 100 ways to do this. But if you do, don't list them all, give me your best shot, just a single one you think is relevant to add here, or your top 3 picks.

Last edited by zeebra; 06-08-2022 at 06:20 AM.
 
Old 06-08-2022, 05:44 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
what do you want to study?
if you have sh bash you can have zsh bash, ksh bash or dash bash or any other combinations of any other shells.
And you can invoke bash from ssh and from a lot of other tools (like perl, python, java, php, ruby, .....).
 
Old 06-08-2022, 06:11 AM   #4
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,834

Original Poster
Blog Entries: 17

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by pan64 View Post
if you have sh bash you can have zsh bash, ksh bash or dash bash or any other combinations of any other shells.
Yes, but sh is actually bash, a subset of bash anyways. And I did considering invoking bash with other shells, but I decided I wanted to stick with bash shell only and not the other ones.

Quote:
Originally Posted by pan64 View Post
And you can invoke bash from ssh and from a lot of other tools (like perl, python, java, php, ruby, .....).
Any specific good examples? (that you can do from tty1 agetty/login/bash)

And to be frank, ssh, maybe, but I wouldn't consider python, java and php good examples. Slackware also doesn't come with java as far as I know. But the point is local execution mainly or at least pseudo local (ssh?). Trying to keep it simple also in my opinion would avoid complex executioners
But executioniers are absolutely relevant, but preferably simple ones. I'm not a big fan of pkexec, but that's one that I can think of at the top of my head.

Last edited by zeebra; 06-08-2022 at 06:26 AM.
 
Old 06-08-2022, 06:59 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by zeebra View Post
Yes, but sh is actually bash, a subset of bash anyways. And I did considering invoking bash with other shells, but I decided I wanted to stick with bash shell only and not the other ones.
No, that is wrong. sh is a different shell, using different syntax. Actually the binary can work (act) as bash and as sh too, but on some systems you may find two different binaries too.
Quote:
Originally Posted by zeebra View Post
Any specific good examples? (that you can do from tty1 agetty/login/bash)
ssh/agetty/login will (may) automatically start bash. What kind of example do you need?

Quote:
Originally Posted by zeebra View Post
And to be frank, ssh, maybe, but I wouldn't consider python, java and php good examples. Slackware also doesn't come with java as far as I know. But the point is local execution mainly or at least pseudo local (ssh?). Trying to keep it simple also in my opinion would avoid complex executioners
But executioniers are absolutely relevant, but preferably simple ones. I'm not a big fan of pkexec, but that's one that I can think of at the top of my head.
Every and each application works the same way, starting a new app is a two-step process: 1. clone the current environment, 2. exec the new binary (= bash in your case).
 
Old 06-08-2022, 07:23 AM   #6
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado, USA
Distribution: Slackware
Posts: 930

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
subshells

When is a subshell created in bash?

pipeline - in a pipeline each command separate by | or |& is executed it's own subshell

control operator - when a command list ends in ampersand & then a subshell is created and runs in the backgound

command grouping () - when commands are grouped by parenthesis () then the list of commands runs in a subshell

command substitution - using $() spins off a subshell

coproc - reserved word, like ampersand & spins off a subshell

programable completion building - the 'complete -C command' spins off a subshell

The list above is a start for your research. I'm sure you'll uncover other causes of subshells as you look into them.
 
1 members found this post helpful.
Old 06-08-2022, 07:23 AM   #7
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,834

Original Poster
Blog Entries: 17

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by pan64 View Post
No, that is wrong. sh is a different shell, using different syntax. Actually the binary can work (act) as bash and as sh too, but on some systems you may find two different binaries too.

ssh/agetty/login will (may) automatically start bash. What kind of example do you need?

Every and each application works the same way, starting a new app is a two-step process: 1. clone the current environment, 2. exec the new binary (= bash in your case).
Ok, I don't have a problem anyways, so there is no stress to get anything solved, just a pleasant conversation about the topic, in theory and in practice.
I hate to argue, but on my system the real execution path of "sh" is /bin/bash, which in my opinion is bash in "sh" mode, or a subset of bash. But well, I could be wrong, but that's how it looks at least to me.

This actually reminds me there is a /usr/bin/bash, which would in the context of this topic be another execution of bash, although it simply points to /bin/bash, but the execution file/context is different.
 
Old 06-08-2022, 07:33 AM   #8
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,834

Original Poster
Blog Entries: 17

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by Chuck56 View Post
When is a subshell created in bash?

pipeline - in a pipeline each command separate by | or |& is executed it's own subshell
Thanks, that is very useful indeed. It makes me rethink my approach. One of the things I did try was a simple script file to execute /bin/bash in a subshell and descend into it and then execute /bin/bash from there and descend into it.
I tried all kind of combinations with arguments -i and -l, but in the end I failed to accomplish what I wanted. I was experimenting quite alot and did it in different ways with () and |, and in one instance I did succeed in doing what I wanted, which was to go from agetty/login/bash and descend directly into a third subshell of bash. But I failed to reproduce it, and I don't know what cause it to succeed.

Later attempts simply created 3 single subshells of bash where I descended into the first one.
 
Old 06-08-2022, 07:36 AM   #9
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
I don't know how slack is configured but in many distributions /bin/sh is just a link to whatever default shell it uses i.e. bash, dash etc. In addition /bin is also just a link to /usr/bin. So basically all paths point to the same thing. It is not a subset or different mode.
 
1 members found this post helpful.
Old 06-08-2022, 07:39 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
we have a variable SHLVL which will show you the "levels" of your subshells.
also you can use the commands env and set to see how is your current shell configured and compare it to another shell or another subshell (if you wish)
 
1 members found this post helpful.
Old 06-08-2022, 07:48 AM   #11
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,834

Original Poster
Blog Entries: 17

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by pan64 View Post
we have a variable SHLVL which will show you the "levels" of your subshells.
also you can use the commands env and set to see how is your current shell configured and compare it to another shell or another subshell (if you wish)
Now that's very helpful for these kind of experiments as they sometimes can get very confusing very fast. Including auditing each action.

Code:
SHLVL=3
Currently. But just from running a couple of minor checks in Konsole.

Last edited by zeebra; 06-08-2022 at 07:51 AM.
 
Old 06-08-2022, 07:53 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,047

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by michaelk View Post
I don't know how slack is configured but in many distributions /bin/sh is just a link to whatever default shell it uses i.e. bash, dash etc. In addition /bin is also just a link to /usr/bin. So basically all paths point to the same thing. It is not a subset or different mode.
bash is able to detect its own invocation (as any other program on linux/unix) and act upon that. So /bin/bash, /bin/sh, /bin/rbash and others may point to the same binary, but using different names will cause different behavior.
It works with vi too, it can also be invoked as vim, ex, view, rvim (for example).
 
Old 06-08-2022, 07:56 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
I knew that but I guess I was unaware that bash had different behaviors...
 
Old 06-08-2022, 07:57 AM   #14
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,834

Original Poster
Blog Entries: 17

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
Quote:
Originally Posted by pan64 View Post
what do you want to study?
It's actually within the context of security
 
Old 06-08-2022, 08:00 AM   #15
Chuck56
Member
 
Registered: Dec 2006
Location: Colorado, USA
Distribution: Slackware
Posts: 930

Rep: Reputation: 479Reputation: 479Reputation: 479Reputation: 479Reputation: 479
Quote:
Originally Posted by michaelk View Post
I knew that but I guess I was unaware that bash had different behaviors...
Yep, "When invoked as sh, Bash enters POSIX mode after reading the startup files."

https://www.gnu.org/software/bash/ma...OSIX-Mode.html
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to invoke a different java version ? sysmicuser Linux - Newbie 12 12-19-2017 07:53 AM
[SOLVED] BASH - Invoke commands when trap is activated worm5252 Programming 1 10-04-2011 04:54 PM
[SOLVED] How to invoke the calculator "bc" in bash script for floating point arithmetic? bluesmodular Programming 2 05-15-2010 11:58 PM
LXer: 15 Ways Nokia’s N900 Is Better Than Apple’s iPhone (and 5 ways it’s not) LXer Syndicated Linux News 0 11-14-2009 08:20 AM
have my .cshrc or .login invoke bash mattie_linux Linux - Newbie 1 06-06-2007 03:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 11:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration