Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script shebang for unknown path

Tags:

shell

shebang

env

Is it possible to specify a shebang line without knowing the path of the program you want to do the executing?

maybe don't specify the path

#!node

or specify several options

#!/usr/local/bin/node
#!/usr/bin/node

Extra points for cross platform solution (various flavors of linux, BSD, OSX etc...)

like image 576
Billy Moon Avatar asked Aug 09 '12 18:08

Billy Moon


People also ask

How do you use shebang in the shell?

Some typical shebang lines: #!/bin/sh – Execute the file using the Bourne shell, or a compatible shell, assumed to be in the /bin directory. #!/bin/bash – Execute the file using the Bash shell. #!/usr/bin/pwsh – Execute the file using PowerShell.

What is the correct shebang line for a bash script?

What is bash shebang? The #! syntax is used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. The directive must be the first line in the Linux shell script and must start with shebang #! .

Can we write shell script without shebang?

shebang line is needed in the file and only if it's meant to be run as executable (as opposed to sh file.sh invocation. It is not actually needed by script, it is for the system to know how to find interpreter.


1 Answers

/usr/bin/env is specifically thought of for cross-platform solutions.

env executes utility after modifying the environment as specified on
the command line.  The option name=value specifies an environmental
variable, name, with a value of value.  The option `-i' causes env
to completely ignore the environment it inherits.

If no utility is specified, env prints out the names and values of
the variables in the environment, with one name=value pair per line.

so something in lines of:

#!/usr/bin/env node

Will be cross-platform and "the right way to go".

like image 123
favoretti Avatar answered Oct 04 '22 07:10

favoretti