LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-13-2016, 06:44 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 275

Rep: Reputation: Disabled
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.

Last edited by fanoflq; 06-13-2016 at 06:45 PM.
 
Old 06-13-2016, 06:57 PM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,652
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
Clearly explained in
Code:
man bash
 
Old 06-13-2016, 07:06 PM   #3
fanoflq
Member
 
Registered: Nov 2015
Posts: 275

Original Poster
Rep: Reputation: Disabled
@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?
 
Old 06-13-2016, 07:06 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,008
Blog Entries: 1

Rep: Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109
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.

Last edited by astrogeek; 06-13-2016 at 07:08 PM. Reason: tpos, typs, typos...
 
2 members found this post helpful.
Old 06-13-2016, 07:15 PM   #5
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,652
Blog Entries: 10

Rep: Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075Reputation: 2075
Quote:
Originally Posted by fanoflq View Post
@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.

Last edited by Habitual; 06-14-2016 at 08:34 AM.
 
Old 06-13-2016, 07:52 PM   #6
fanoflq
Member
 
Registered: Nov 2015
Posts: 275

Original Poster
Rep: Reputation: Disabled
@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.

Last edited by fanoflq; 06-13-2016 at 08:01 PM.
 
Old 06-14-2016, 12:31 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,008
Blog Entries: 1

Rep: Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109Reputation: 2109
Quote:
Originally Posted by fanoflq View Post
@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 View Post
Again, thank you for your generous time.
You are welcome.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
pros and cons -- small "linux box server" versus dedicated "NAS box" SaintDanBert Linux - Server 1 11-30-2013 04:29 PM
"-bash: et: command not found" when using "su -" cal7 Linux - General 5 08-04-2011 03:09 PM
"Shared physical device" versus "Virtual network" ThomasRonshof Linux - Virtualization and Cloud 2 06-17-2011 04:27 PM
Bash Conditions: "(" versus "[" mstng_67 Programming 7 07-12-2010 07:08 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM


All times are GMT -5. The time now is 10:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration