Explain: #!/bin/bash - or #!/bin/bash -- In A Shell Script

Posted on in Categories , , , , , , , last updated September 23, 2008

Q. I know #!/bin/bash is shebang line. But, I noticed a few shell script shebang line ends with a single dash ( #!/bin/bash ) or double dash ( #!/bin/bash ). Can you explains me purpose of such shebang line?

A. A – or — signals the end of options and disables further option processing i.e. bash will not accept any of its option. Any arguments after the — are treated as filenames and arguments. An argument of – is equivalent to –. This is done to improve script security. Some user may perform setuid based script root spoofing. To avoid interpreter spoofing you need to add — to #!/bin/bash. This is rare but possible attack.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

12 comment

  1. man bash:

    ” — A — signals the end of options and disables further option processing. Any argu ments after the — are treated as filenames and arguments. An argument of – is equivalent to –.

  2. you can set whatever prompt you like with

    set PS1=”[newprompt]”; export PS1

    but you’ll have to set the prompt every time you log in, unless you can edit the hidden bash profile that should be in your home dir

  3. #!/bin/bash
    SERVERIP=192.168.23.18
    LOGFILE=/home/user/backup.log
    rsync -avz –delete –numeric-ids \
    –exclude=”.Trash” \
    –exclude=”Downloads” \
    $HOME $SERVERIP:backups/user/ 1>$LOGFILE

    what does this mean?

Leave a Comment