What is "#!' in a script file? [duplicate]

$cat shebang.sed 
#! /bin/sed -f
s/red/xxx/
s/BLUE/1234/

$./shebang.sed rgb
lower (#1): "xxx green blue"
UPPER (#2): "RED GREEN 1234"

$sed -f shebang.sed rgb
lower (#1): "xxx green blue"
UPPER (#2): "RED GREEN 1234"

I may guess what #! is doing. However, I don't say I know it. what is "#!"?


Solution 1:

This line is called shebang and is used to tell the terminal which interpreter to use when executing the script. In your case, you use sed

Other examples would be:

#!/bin/bash            # -> uses bash
#!/usr/bin/perl        # -> uses perl
#!/usr/bin/env python  # -> uses python