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?
Why do I need #!/bin/bash ?
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
Why do I need #!/bin/bash ?
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.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: Why do I need #!/bin/bash ?
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).
As to what #! does, see https://en.wikipedia.org/wiki/Shebang_(Unix).
Re: Why do I need #!/bin/bash ?
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
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
Re: Why do I need #!/bin/bash ?
When you execute a file with, for example,
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:
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.
./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.
Re: Why do I need #!/bin/bash ?
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)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?
Re: Why do I need #!/bin/bash ?
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 shebangtuxoneseven wrote: ⤴Tue Mar 23, 2021 8:03 am There are many shells available, bash is just one of them.
#!/bin/cat
and you'll see what I mean.I'm also Terminalforlife on GitHub.
Re: Why do I need #!/bin/bash ?
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
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
Code: Select all
$ bash
$ PS pcmanfm
PS pcmanfm
... pcmanfm -n /mnt/DATA/download
... grep -i pcmanfm # <-- no 'sh' process
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?
Your data and OS are backed up....right?
Re: Why do I need #!/bin/bash ?
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?
Your data and OS are backed up....right?
Re: Why do I need #!/bin/bash ?
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 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.