Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dash '-' after #!/bin/sh -

Tags:

shell

sh

centos

I have been working on a few scripts on CentOS 7 and sometimes I see:

#!/bin/sh -

on the first line. Looking at the man page for sh I see the following under the Special Parameters

   -      Expands to the current option flags as  specified  upon  invocation,
          by  the  set  builtin  command, or those set by the shell
          itself (such as the -i option).

What exactly does this mean? When do I need to use this special parameter option??

like image 938
GoinOff Avatar asked Jul 19 '19 16:07

GoinOff


1 Answers

The documentation you are reading has nothing to do with the command line you're looking at: it's referring to special variables. In this case, if you run echo $- you will see "the current option flags as specified upon invocation...".

If you take a look at the OPTIONS part of the bash man page, you will find:

--       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 --.

In other words, an argument of - simply means "there are no other options after this argument".

You often see this used in situation in which you want to avoid filenames starting with - accidentally being treated as command options: for example, if there is a file named -R in your current directory, running ls * will in fact behave as ls -R and produce a recursive listing, while ls -- * will not treat the -R file specially.

The single dash when used in the #! line is meant as a security precaution. You can read more about that here.

like image 169
larsks Avatar answered Oct 25 '22 18:10

larsks