What shebang?
Welcome, Guest.
He could not keep viruses out of Windows! Do you want Bill Gates' MANDATORY Covid-19 Vaccine? Sign this petition!
"Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety." -Benjamin Franklin

Author Topic: What shebang?  (Read 2774 times)

Offline edge226

  • Full Member
  • ****
  • Posts: 285
What shebang?
« on: April 29, 2016, 04:31:26 PM »
Hey lets talk a little about the shebang at the top of a bash file.

Did you know there are different types?
Did you know those types could affect the security of your script?

There are two different types of shebangs for bash they are as follows:

## Direct link examples. There are multiple because it may be done differently in different Linux distros.


Code: [Select]
## Direct bash call shebang.
## Shebang of a distro with symlinked /bin -> /usr/bin, /sbin -> /usr/sbin, etc.
## Arch fits in this category.

#!/usr/bin/bash

## Non Symlinked distro such as ubuntu or debian.

#!/bin/bash


Doing your shebang the above way is not the best way in most cases.
Through env.

Code: [Select]
#!/usr/bin/env bash

Doing your shebang this way creates the best portability within your system.

Now I mentioned security but I didnt really mention why these two had differences in security, Here it is!
If I check the man page for env or run whatis env I get something similar to this. Whatis listed more but this was the listing that I made most sense and corresponds with the man page.
env - run a program in a modified environment

The info I am about to put in here is available via the env man page or env --help:
Code: [Select]
$ env --help
Usage: env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
Set each NAME to VALUE in the environment and run COMMAND.

Since we sent no NAME=VALUE fields when we run the shebang this should start our script with a clean environment.

Offline Spatry

  • Benevolent Dictator
  • Administrator - Sysop
  • **********
  • Posts: 5638
  • Cup of Linux Founder
    • Cup of Linux
Re: What shebang?
« Reply #1 on: April 29, 2016, 04:38:08 PM »
And I thought "shebang" was a leprechaun fornicating. Thanks for the tutorial, Edge!
Windows assumes the user is an idiot... Linux DEMANDS Proof!

Offline JayVii

  • Contributor
  • *******
  • Posts: 1759
    • JayVii's Page
Re: What shebang?
« Reply #2 on: April 30, 2016, 05:02:28 AM »
Just to make sure, I understand it correctly:
"Clean environment" means "no environment variables" (for example if i exported $TERM from my ~/.bashrc)?

Offline edge226

  • Full Member
  • ****
  • Posts: 285
Re: Re: What shebang?
« Reply #3 on: April 30, 2016, 02:29:30 PM »
Just to make sure, I understand it correctly:
"Clean environment" means "no environment variables" (for example if i exported $TERM from my ~/.bashrc)?

There is an option for env that does "Clear" your environment variables. This just makes sure there is no cross talk like someone setting a variable and then it being put into your Script because they set the variable prior to running the script.