• #! SHEBANG issues


    Hello,

    I’ve learned how to run a program I wrote without using the command bash from the book The Unix Workbench by making the file executable, which I did, and writing the shebang on the first line of the script:

    #!/usr/bin/env bash

    It more or less worked during the example but when I tried to do the same on one of my own script using nano for writing it didn’t worked.

    I also have to do it for python 3.7 scripts, which seems to be harder to make it works. The example I found on the python website was:

    #!/usr/bin/env python

    Could someone explain me how this part of the script works and give me an example that I can apply?

    Thanks

  • @ReversibleMan I’m no programmer but I always use:

    #!/bin/bash

    in my shell scripts and they always work.

  • bash : unknown command

    I now use Atom to write the program but it doesn’t work.

    I tried almost every path I found in the $PATH variable:

    #!/usr/local/bin bash
    #!/usr/bin bash
    #!/bin bash
    #!/usr/local/sbin bash

    or

    #!/usr/local/bin/bash
    #!/usr/bin/bash
    #!/bin/bash
    #!/usr/local/sbin/bash

  • @ReversibleMan

    What’s the output of this command:

    which -a bash
    

    The traditional #!/bin/bash on the first line works for me.
    It is not actually required at all, but may help some programs (like certain editors) that rely on it.

    You must also make sure your script really is executable:

    chmod +x "your-script"
    
  • @manuel It’s required by KDE: if I want to run a shell-script by right-clicking and running in terminal it fails without it.
    Like I say I’m no programmer, just sayin’ :-)

  • AFAIK using #/usr/bin/env <executable> is the portable way of defining the interpreter to use for scripts. I use portable in the sense that executable is more likely to be found on a wider variety of systems.
    This was certainly an issue that I encountered in the past, because the location of executables were not the same in all Linux distros or BSDs and I often use this way to specify bash or python in my script files.
    Not sure how much of an issue this is today.
    The primary purpose of env is to run commands in a modified environment see man or info pages for more details.

  • which -a bash gives me:

    /usr/bin/bash
    /bin/bash

    But none of these worked

    My file is executable at all level of users

    I’m pretty sure the problem is about the path and not the shebang line on the editor because the command is recognised.

    I looked at the man page for env and tried the

    /usr/bin/env -v

    Which gave me a lot of informations like the path for the shell:

    /bin/bash

    (but this one didn’t work) and other arguments.

    So at this time I can execute the file without entering bash but I have to specify the path to it, even if I’m in the correct folder.

  • @ReversibleMan
    So you have a script e.g. at $HOME and you wish to do this:

    cd
    "my-script"
    

    Right?
    If so, it doesn’t work because your script simply is not on $PATH. In Windows that would work, but no on Linux. But it works if you command:

    cd
    ./"myscript"
    

    provided it is marked as executable. So you must give the path too.

    If this is not your situation, please give a clear and precise example of what you are trying to do.

  • I investigated more last night and the problem isn’t about the shebang but about the environmental variable. A line was missing in the book and I thought that adding the shebang could let your run the script without having to enter the path to the script.

    Anyway I learned new things.

    I know how to enter it in the $PATH variable and through the ~/.bash_profile so I can run the script without having to enter the path, but the question is, which is another topic, how can I do it automatically for other people so they can run the script without having to enter the path if I give it to them, juste as programs like Nmap.

  • @ReversibleMan

    how can I do it automatically for other people so they can run the script without having to enter the path

    How to do that, depends very much on how other people can use your definitions.

    For example, you may put your script to /usr/local/bin and make it executable:

    su
    chmod +x "your-script"
    chown root:root "your-script"
    mv "your-script" /usr/local/bin
    

    Or you may write a function that calls your script and put it into file /etc/bashrc.bash:

    my_function() { /path/to/your-script "[email protected]" ; }
    

    Then all bash users in the same machine get this function and can easily call it by name like my_function.

    Here is some great documentation:

    man bash
    
  • Thanks, I’ll try this in the script I’m writing.

issues90 shebang2 Posts 11Views 139
Log in to reply
Bloom Email Optin Plugin

Looks like your connection to Antergos Community Forum was lost, please wait while we try to reconnect.