Why do I need #!/bin/bash ?

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
jave
Level 4
Level 4
Posts: 227
Joined: Mon Dec 02, 2019 6:53 am

Why do I need #!/bin/bash ?

Post by jave »

Hi,

real noob question here.

I can write a simple text file with line commands to be executed one by one. I can then chmod it to make it executable. But why to some script files include the "#!/bin/bash" command at the start when my text file WITHOUT "#!/bin/bash" still executes and works?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29606
Joined: Wed Jul 06, 2011 3:58 am

Re: Why do I need #!/bin/bash ?

Post by xenopeek »

Because there are many other scripting languages. If you don't specify #!/bin/bash and you run a executable script it runs with the default shell command interpreter. That may not be bash and then it would break your bash script.

As to what #! does, see https://en.wikipedia.org/wiki/Shebang_(Unix).
Image
User avatar
AndyMH
Level 21
Level 21
Posts: 13736
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Why do I need #!/bin/bash ?

Post by AndyMH »

https://en.wikipedia.org/wiki/Shebang_(Unix)

As to why some scripts run without a shebang, no idea.

And xenopeek beat me to it with the link :)
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Why do I need #!/bin/bash ?

Post by Termy »

When you execute a file with, for example, ./file.sh, the system (I believe it's technically the kernel which handles this) will look for a shebang to tell it what to use to execute the file, such as Bourne Shell, BASH, ZSH, CSH, TCSH, FISH, Perl, Python, Ruby, even AWK. There are loads. It doesn't just tell the system what, but also where.

The reason you can get away with not using a shebang, is if you explicitly provide the executable yourself when running the file, such as: sh file.sh

Another reason you might want to use a shebang is for your text editor's syntax highlighting, which might not kick in properly if it's omitted.
I'm also Terminalforlife on GitHub.
tuxoneseven

Re: Why do I need #!/bin/bash ?

Post by tuxoneseven »

jave wrote: Mon Mar 15, 2021 4:51 pm Hi,

real noob question here.

I can write a simple text file with line commands to be executed one by one. I can then chmod it to make it executable. But why to some script files include the "#!/bin/bash" command at the start when my text file WITHOUT "#!/bin/bash" still executes and works?
There are many shells available, bash is just one of them. One popular alternative is Zsh https://www.zsh.org/. All Linux systems use bash now, but like anything Linux it's not the only option. Some systems use others by default. OpenBSD uses ksh instead, because it has fewer lines of code (less chance of vulnerability)
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Why do I need #!/bin/bash ?

Post by Termy »

tuxoneseven wrote: Tue Mar 23, 2021 8:03 am There are many shells available, bash is just one of them.
To avoid a misunderstanding, I want to outline that the shebang is for an executable (optionally with arguments) in general, not exclusively for shells or even interpreters. Create and execute a new file with the shebang #!/bin/cat and you'll see what I mean.
I'm also Terminalforlife on GitHub.
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Why do I need #!/bin/bash ?

Post by Flemur »

Termy wrote: Mon Mar 15, 2021 7:51 pm The reason you can get away with not using a shebang, is if you explicitly provide the executable yourself when running the file, such as: sh file.sh
I noticed something with zsh, that when I run an executable script with just "scriptname" I see it's run with sh, but in bash there's no sh:

Code: Select all

$ cat bin/PS
ps aux | grep -i $1 | cut -c1-150
in zsh:

Code: Select all

$ PS pcmanfm
... pcmanfm -n /mnt/DATA/download
... sh /home/username/bin/PS pcmanfm   # <-- shows this sh process (sh=dash).
... grep -i pcmanfm
But in bash:

Code: Select all

$ bash
$ PS pcmanfm
PS pcmanfm
... pcmanfm -n /mnt/DATA/download
... grep -i pcmanfm  # <-- no 'sh' process
If I add #!/bin/bash to the PS file, I get the additional script process in bash and zsh:

Code: Select all

/bin/bash /home/username/bin/PS pcmanfm
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
Flemur
Level 20
Level 20
Posts: 10096
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: Why do I need #!/bin/bash ?

Post by Flemur »

AndyMH wrote: Mon Mar 15, 2021 5:21 pmAs to why some scripts run without a shebang, no idea.
See that post I just made; when I run a script which does not have the shebang, there's no "bash" or "sh" process when running bash in the terminal, but there is one ("sh") if I run the same script in a zsh session.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
User avatar
Termy
Level 12
Level 12
Posts: 4248
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Why do I need #!/bin/bash ?

Post by Termy »

I see something weird too, which I think might be the same thing to which you're referring:

I used watch(1) on a script which does just a 30s sleep(1). With the script using a Bourne Shell shebang (DASH, in my case) no new process is listed by watch(1), but if I change the shebang to a BASH one, a new BASH process is listed. Is a bit weird. I'd have expected DASH to show up with the Bourne Shell variant, since '/bin/sh' is a symbolic link to DASH, as most of us are aware.
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”