LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Command: bash -c "some_command_line" versus ONLY some_command_line (https://www.linuxquestions.org/questions/linux-newbie-8/command-bash-c-some_command_line-versus-only-some_command_line-4175582187/)

fanoflq 06-13-2016 05:44 PM

Command: bash -c "some_command_line" versus ONLY some_command_line
 
I have not found an explanation of
the need to use command like so:

Code:

bash -c "your_command_line"
versus just this:

Code:

your_command_line
Until now, both execution works.
Below is the first time I see why using
prefix "bash -c" in a command line is needed.

Code:

user@host /proc/sys/kernel $ cat /proc/sys/kernel/threads-max
100000

#This does not work!
user@host /proc/sys/kernel $ sudo echo 120000 > /proc/sys/kernel/threads-max
bash: /proc/sys/kernel/threads-max: Permission denied

# This works!
user@host /proc/sys/kernel $ sudo bash -c 'echo 120000 > /proc/sys/kernel/threads-max'

user@host /proc/sys/kernel $ cat /proc/sys/kernel/threads-max
120000

I like to know when to use "bash -c" as prefix in a command line,
and why it is needed.

Thank you.

Habitual 06-13-2016 05:57 PM

Clearly explained in
Code:

man bash

fanoflq 06-13-2016 06:06 PM

@Habitual
Thank you for your great habits of using man pages,
and bringing awareness of the great man pages
every chance you got.

Great. There is no need for www.linuxquestions.org
since man pages very clearly explain everything.

Time to shut down www.linuxquestions.org.
Agree? Yes? No?

astrogeek 06-13-2016 06:06 PM

That happens because the output redirection is handled by the shell within which you are executing the command, not by the command being issued.

So if root owns the target of the redirect...
Code:

-rw-r--r-- 1 root  root        0 Jun 13 16:54 rfile

then...

echo "some text" >rfile
bash: rfile: Permission denied

AND...

sudo echo "some text" >rfile
bash: rfile: Permission denied

Because only the command, echo "some text" is executed with or without sudo permission, whereas the redirection, > rfile, is always handled by the shell you are working in which does not itself have sudo permission.

But like this...
Code:

sudo bash -c "echo 'some text' >rfile"
...you are opening a new shell which does have sudo permission, and everything exeuted within that new execution shell, including the redirect, happens with those permissions.

Habitual 06-13-2016 06:15 PM

Quote:

Originally Posted by fanoflq (Post 5560412)
@Habitual
Thank you for your great habits of using man pages,
and bringing awareness of the great man pages
every chance you got.

Great. There is no need for www.linuxquestions.org
since man pages very clearly explain everything.

Time to shut down www.linuxquestions.org.
Agree? Yes? No?

You're welcome.
Actually, if more users like yourself had learned to search the existing documentation,
there would be nothing for me to do. So help me break the habit and read some.

End Transmission.

fanoflq 06-13-2016 06:52 PM

@astrogeek:

Thank you.
That sounds logical.

I understand your explanation like so:
(this is redundant, for newbie clarification and reference only)

There are 2 commands (processes) running in command :

Quote:

sudo echo 120000 > /proc/sys/kernel/thread-max
For 1st command, sudo echo 120000, it is one process and
it is given sudo privileges ( sudo not really needed here).

Then when it completes and returns to Bash shell,
the first operation result is to be redirected as part of the second command (to be executed from and BY the Bash shell).

However, the Bash shell does not have sudo privileges.
that is why access to /proc/sys/kernel/thread-max was denied.

So in order to run these sequential commands (some of which
may need sudo privileges down the commands chain) we need to
run them in a shell with elevated privileges.
Hence the need to launch privilege Bash shell like so:
Quote:

sudo bash -c "your_command_chain_quotes_included"
Again, thank you for your generous time.

astrogeek 06-13-2016 11:31 PM

Quote:

Originally Posted by fanoflq (Post 5560431)
@astrogeek:

Thank you.
That sounds logical.

I understand your explanation like so:
...
So in order to run these sequential commands ... we need to
run them in a shell with elevated privileges.
Hence the need to launch privilege Bash shell...

More or less... but the particular thing here is that the redirect itself, the '>', is performed by and within the environment of the original shell regardless of any commands that preceed or follow it, so it must satisfy the permissions required by its target(s).

Quote:

Originally Posted by fanoflq (Post 5560431)
Again, thank you for your generous time.

You are welcome.


All times are GMT -5. The time now is 09:42 PM.