Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [shebang]

Use the tag /shebang if your question is about `#!`-string at the beginning of a script

4
votes
6answers
3k views

shebang or not shebang

I want to use a program in the shebang, so I create a script named <myscript> with: #!<mypgm> I also want to be able to run <mypgm> directly from the command prompt. <mypgm> ...
3
votes
3answers
80 views

Who runs the interpreter for files that are execute-only?

If the current user only has execute (--x) permissions on a file, under which user does the interpreter (specified by #!/path/to/interpreter at the beginning of the file) run? It couldn't be the ...
15
votes
6answers
2k views

Is it correct to use /bin/sh in the hashbang if the Bourne shell isn't available in a distribution?

Generally, shell scripts contain the following comment at the first line of the script file: #!/bin/sh. According to the researches that I made, this is called "hash bang" and it is conventional ...
17
votes
3answers
2k views

Why is the behavior of the `#!` syntax unspecified by POSIX?

From the Shell Command Language page of the POSIX specification: If the first line of a file of shell commands starts with the characters "#!", the results are unspecified. Why is the behavior of #...
14
votes
2answers
846 views

How does this shebang that starts with a double hyphen (--) work?

I have found the following kind of shebang in the RosettaCode page: --() { :; }; exec db2 -txf "$0" It works for Db2, and a similar thing for Postgres. However, I do not understand the whole line. ...
1
vote
1answer
80 views

Why does a script without she-bang can be run? [duplicate]

As far as I understand, to make kernel execve a non-ELF file, the file must be a script started with a she-bang #!, but I have a script run successfully without it, why does this happen? xtricman⚓...
1
vote
5answers
304 views

Separate environment with different /bin/sh

I have a bunch of shell scripts which incorrectly assume /bin/sh to be equivalent to /bin/bash. E.g., they have the #!/bin/sh shebang, but use the source command instead of . (dot). I run Ubuntu 16, ...
3
votes
0answers
104 views

Why do I need this shebang? [duplicate]

perldoc perlrun contains this example: #!/bin/sh #! -*-perl-*- eval 'exec perl -x -wS $0 ${1+"$@"}' if 0; But I don't understand why it might be needed. For example, if my perl interpreter ...
0
votes
2answers
73 views

Why does shebang lead to a different process name than an explicit call?

Summary Using mirage as an example, a python program that begins with a shebang: #!/usr/bin/python ... Looking at /proc/<pid>/comm or using pgrep, it appears like ... ... the process name ...
13
votes
3answers
1k views

Why doesn't “ps ax” find a running bash script without the “#!” header?

When I run this script, intended to run until killed... # foo.sh while true; do sleep 1; done ...I'm not able to find it using ps ax: >./foo.sh // In a separate shell: >ps ax | grep foo.sh ...
1
vote
0answers
78 views

Upstart script with expect scripting

Please give me advices about expect scripting combined with an upstart script. There are 3 scripts that I am dealing with right now. Script A) Java program PATH: /opt/~~/manager Script B) ...
0
votes
1answer
247 views

Run python script without declare it interpreter

I have such a program to check methods of data from the command line: me at me in ~/Desktop/Coding/codes $ cat check_methods.py #! /usr/bin/env python from sys import argv methods = dir(eval(argv[1]))...
3
votes
1answer
134 views

How is a bash script executed via its filename as command name with and without shebang?

From bash manual: 3.7.2 Command Search and Execution After a command has been split into words, if it results in a simple command and an optional list of arguments, the following actions are ...
0
votes
1answer
468 views

/usr/bin/env: ‘awk -f’: No such file or directory [duplicate]

Linux shylock-archinux 4.15.15-1-ARCH #1 SMP PREEMPT Sat Mar 31 23:59:25 UTC 2018 x86_64 GNU/Linux My machine is above.Anyone know what happend? The script with shebang #! /usr/bin/env awk -f don't ...
1
vote
2answers
64 views

How shall I allow more than one arguments to the interpreter in a shebang in a script

From Glenn's reply: Read your execve(2) man page. The limitation on a single optional argument is OS dependent. Linux treats all words after the interpreter as one single argument If you ...
1
vote
1answer
186 views

Basic usage of /etc/cron/ (d): correct pattern for /etc/cron (daily/weekly/monthly)

I use Ubuntu 16.04 with Bash and I've created this extensionless, shebangless file /etc/cron.daily/cron_daily: for dir in "$drt"/*/; do if pushd "$dir"; then wp plugin update --all --allow-root wp ...
1
vote
2answers
416 views

What are the pro's and con's in using the “-l” in a script shebang

I recently came up to an easy fix for a crontab logging issue and I am wondering what are the pro's and con's of using this specific fix (running a script with a "login shell flag"), as: #!/bin/bash -...
11
votes
6answers
2k views

Too many shebang (script declaration) lines — any way to reduce their amount?

I have a project comprised of about 20 small .sh files. I name these "small" because generally, no file has more than 20 lines of code. I took a modular approach because thus I'm loyal to the Unix ...
0
votes
0answers
428 views

shebang and command parameters [duplicate]

I tried to pass parameters to bash in shebang of executable script: #! /usr/bin/env bash -ex false But I get an error: /usr/bin/env: ‘bash -ex’: No such file or directory Should I avoid to use /...
2
votes
1answer
184 views

cannot get scripts to run in a Lubuntu (Xenial) Minimal (+LXDE) VM with shebangs

I embarrassed myself a little here with a simple typo and a profound ignorance. Save yourself some grief: your hasbangs/shebangs must always have a leading /, such as #!/bin/bash be precise if you ...
2
votes
5answers
433 views

Fastest way to determine if shebang is present

If I have a file with #!/usr/bin/env foobar what is the fastest/best way to determine if this file has a hashbang? I hear you can just read the first 2 bytes? How?
6
votes
3answers
902 views

Can I use a variable content as a shebang? [closed]

I have a shell script in which I want to add a shebang. Given a variable defined as follows: SHEBANG="#!/bin/sh" My question is if I can use that variable in another script like this: $SHEBANG # ...
0
votes
1answer
199 views

Change the default executable for file with potentially missing shebang

I have a library - users are to create executable files, potentially with a hashbang to tell exec which executable to use. If they omit the hashbang, then I think most systems default to /bin/sh, I ...
0
votes
1answer
304 views

#!/usr/bin/env foo #!vs /usr/bin/foo [duplicate]

Which of the above forms is "better" for running bash, python etc. scripts? Why can't I just do #!$(which foo)? Is it neccecery to specify full path to env? I gather from this answer, that the path /...
3
votes
2answers
539 views

xslt shbang: Using xslt from the command line

I have (foolishly?) written a couple of moderately general-purpose xslt scripts. I'd quite like to turn these into executables that read an xml document from standard in or similar. The way you do ...
24
votes
6answers
6k views

Multiple arguments in shebang

I am wondering whether there is a general way of passing multiple options to an executable via the shebang line (#!). I use NixOS, and the first part of the shebang in any script I write is usually /...
0
votes
2answers
206 views

What is the actual command executed when running a script with a shebang as its name with same arguments?

From bash manual about shebang Most versions of Unix make this a part of the operating system’s command execution mechanism. If the first line of a script begins with the two characters‘ #!’, ...
3
votes
1answer
953 views

Avoid passing environment variable to python script

I have a small python script #!/usr/bin/env python3 import some_python3_module def main(): # do stuff if __name__ == '__main__': main() and cannot run this script with Python3, since ROS ...
2
votes
1answer
417 views

Multiple arguments in shebang lines

Linux only supports one argument in a shebang line: This: #!/bin/sh cat > pr_args <<'EOF' #!/bin/sh -e printf "'%s'\n" "$@" EOF cat > shebang <<'EOF' #!pr_args a b c EOF chmod +x ...
2
votes
1answer
256 views

validity check all files in a folder depending on the shebang [closed]

I have a project with some lua and some bash files. I want to loop over all files and depending on the shebang I want to execute a validity check.
14
votes
3answers
5k views

Which shell interpreter runs a script with no shebang?

Suppose the default shell for my account is zsh but I opened the terminal and fired up bash and executed a script named prac002.sh, which shell interpreter would be used to execute the script, zsh or ...
16
votes
6answers
3k views

Choose interpreter after script start e.g. if/else inside hashbang

Is there any way to dynamically choose the interpreter that's executing a script? I have a script that I'm running on two different systems, and the interpreter I want to use is located in different ...
4
votes
2answers
2k views

Why am I able to pass arguments to /usr/bin/env in this case?

I read in another answer that I'm not able to pass arguments to the interpreter than I'm giving to /usr/bin/env: Another potential problem is that the #!/usr/bin/env trick doesn't let you pass ...
0
votes
3answers
574 views

Find all scripts with a given shebang line with find & sed

I want to find out all scripts with a specific shebang line. Specifically, I want all files that match the following criteria: It's mostly a plain text file (stuffs created by gzexe don't look very ...
0
votes
2answers
125 views

Find the path of kermit directory to put after shebang (#!) in script

I used the following script to connect to my serial device using kermit: #!/usr/bin/kermit set port /dev/ttyUSB8 set speed 115200 set carrier-watch off set flow-control none set prefixing all OUTPUT ...
25
votes
1answer
3k views

At sign after shebang?

In the Apache httpd project's "support/apxs.in" script, a text surrounded with @ signs comes after #!. That is, the first line of the script is: #!@perlbin@ -w Is this a Perl thing or a UNIX kernel ...
2
votes
1answer
686 views

Bash here-documents and shebang lines

Is it true to conclude that when using a bash here-doc like bash << HEREDOC, then always and without exceptions, shebang lines like #!/bin/bash -x are redundant? If I would have to bet, I would ...
0
votes
1answer
3k views

Yet another syntax error near unexpected token `(' [closed]

This one different from the others as: I'm invoking bash (not sh) in the shebang: #! /bin/bash I'm running it with ./ File permissions are correct: 755 The line in question is: formattedTIME=`awk '...
3
votes
4answers
2k views

Can I use a shebang to have a file source itself into current bash environment?

I have a growing collection of scripts which should be sourced, not run. At the moment they have the shebang #! /bin/cat but I would prefer the have them be sourced into bash when run, in the same ...
5
votes
1answer
955 views

How should I handle the shebang when writing my own shell?

I'm writing a script to test a shell project to see that my custom shell has correct output. str="HELLO" echo $str echo "*** YOU SHOULD SEE HELLO ABOVE ***" ls * echo "*** YOU SHOULD SEE THE OUTPUT ...
83
votes
1answer
9k views

Why does the following script delete itself?

If you create an executable file with the following contents, and run it, it will delete itself. How does this work? #!/bin/rm
1
vote
2answers
175 views

How can I forward #! through a bash script to provide indirection

Sorry if the question statement wasn't very clear. I'd like to have a bash script that I can edit to change the behavior of a bunch of other scripts. For instance--I have some groovy scripts. they ...
38
votes
2answers
5k views

Is space allowed between #! and /bin/bash in shebang?

In a shebang, is a space or more allowed between #! and the interpreter? For example, #! /bin/bash. It seems work, but some said that it is incorrect.
0
votes
1answer
2k views

How to get the path of a current running script file?

Knowing that program's PID, I want to list out the executable file of a running program. For binary programs, I can read /proc/$pid/exe, which is a symlink to the executable. However for scripting ...
32
votes
4answers
4k views

What exactly happens when I execute a file in my shell?

So, I thought I had a good understanding of this, but just ran a test (in response to a conversation where I disagreed with someone) and found that my understanding is flawed... In as much detail as ...
3
votes
2answers
1k views

`sudo -u user` in shebang line

I'd like to be able to run a script as another user, and only as that user. The way I currently have this set up is to have alice ALL = (bob) NOPASSWD: /home/alice/script.sh in the sudoers file ...
9
votes
2answers
25k views

Why is #!/usr/bin/env bash not working on my system?

I ran into some issues when running some installation scripts where they complained of bad interpreter. So I made a trivial example but I can't figure out what the problem is, see below. #!/usr/bin/...
2
votes
1answer
129 views

Does a doublesharp always cancel out a shebang?

Would an extra # always cancel out the sharp exclamation? As in: Enabled: #!/foo/bar Disbabled: ##!/foo/bar Are there any known circumstances where an exec could execute the bar? Are there ...
2
votes
1answer
193 views

See if a script is running when using #!/usr/bin/env

I have tried migrating the shebang for my bash scripts from #!/bin/bash to #!/usr/bin/env bash, and some of them were broken because they relied on this code that checks for existing instances of ...
53
votes
6answers
5k views

Is there any reason to have a shebang pointing at /bin/sh rather than /bin/bash?

In most shell scripts I've seen (besides ones I haven't written myself), I noticed that the shebang is set to #!/bin/sh. This doesn't really surprise me on older scripts, but it's there on fairly new ...