We help IT Professionals succeed at work.

what is the role of "bash -s"?

beer9
beer9 asked
on
Medium Priority
1,192 Views
Last Modified: 2015-05-15
I see following line to install chefdk

curl -L https://chef.io/chef/install.sh | sudo bash -s -- -P chefdk

Open in new window


I want to understand that does "bash -s -- -P chefdk" does and how does '-P' gets supplied as option to 'install.sh' and not to 'bash' ?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
man bash
...
       -s        If the -s 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.
...
       --        A -- signals the end of options and disables further option processing.  Any arguments after the -- are treated as filenames and  arguments.   An  argument  of  -  is
                 equivalent to --.
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013

Commented:
-s makes bash read commands (the "install.sh" code as downloaded by "curl") from stdin, and accept positional parameters nonetheless.

"--" lets bash treat everything which follows as positional parameters instead of  options.

bash will set the variables "$1" and "$2" of the "install.sh" code to "-P" and to "chefdk", respectively.
CERTIFIED EXPERT

Commented:
Hi woolmilkporc,

> "bash will set the variables "$1" and "$2" of the "install.sh" code to "-P" or to "chefdk", respectively."

Does this mean that after running that whole one-liner, the 1st & 2nd "words" (for lack of a better word) of the output of the curl command will be accessible via $1 & $2?

Also, any ideas why "sudo" is being used here?  Does it imply the user doesn't even have access to run bash without sudo?
ozo
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015

Commented:
Does this mean that after running that whole one-liner, the 1st & 2nd "words" (for lack of a better word) of the output of the curl command will be accessible via $1 & $2?
No
Also, any ideas why "sudo" is being used here?
Perhaps so that the commands in https://chef.io/chef/install.sh can be run as root
Does it imply the user doesn't even have access to run bash without sudo?
No
CERTIFIED EXPERT
Most Valuable Expert 2013
Top Expert 2013
Commented:
The curl command will download a script ("install.sh") and the bash command will execute it on the fly. "-P" and "chefdk" will be presented to this script as if they had been specified on the command line of "install.sh".

It's just the same as if you downloaded "install.sh" and ran it like:

install.sh -P chefdk

Let's make an easy example. Assume that the code of "install.sh" would consist of just two statements to print out the commandline parameters: 'echo $1' and 'echo $2'. Feeding this to the bash statement in question:

echo 'echo $1; echo $2' | bash -s -- -P chefdk

will result in

-P
chefdk


being displayed.

"sudo" is certainly used because the "install.sh" code contains commands which must be run with root privileges or because it reads/writes from/to files/directories which only root can access.
The user can well run bash without sudo, but for the above reasons a bash started with root privileges is required.

Note: The script install.sh will most probably run something like "getopts" to parse its options and their arguments, so the variables "$1" and "$2" will not necessarily be used - although the script could use them, their content will be as described.
CERTIFIED EXPERT

Commented:
Thanks ozo for a concise answer, as usual.

And thanks even more for a well explained answer, woolmilkpork.