How to use multiple arguments in shebang lines

env command allows to find a script’s interpreter when its location is a non-standard one. In scripts it’s recommended to use env in shebang lines to find the executable and run it.

#!/usr/bin/env ruby

puts "Hello, world."
Ruby interpreter is used without any option.

On GNU/Linux if it’s necessary to run a script’s interpreter with options, env has to be used with -S to pass options in shebang lines.

#!/usr/bin/env -S ruby -Ilib:test:.

puts "Hello, world."
Ruby interpreter is used with `-I` option to specify $LOAD_PATH directories.

env could be also used with additional -v option. It’s usedful for debugging.

#!/usr/bin/env -vS ruby -Ilib:test:.

puts "Hello, world."

When executed, the script from above would produce the following output:

#env executing: ruby
#env    arg[0]= 'ruby'
#env    arg[1]= '-Ilib:test:.'
#env    arg[2]= 'bin/test.sh'
Hello, world.

Read next