Can you put multiple executable scripts in one directory, and by sourcing that directory make all of those commands available?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












I want to put a bunch of executable scripts in the .command dir (which is also executable), and then only have to source that directory in my .bash_profile. Is this possible? I can get this to work with one file. But when adding a second file, the second file's commands aren't available in the shell.



my .bashprofile



source ~/.commands/*


my .commands folder



-rwxr-xr-x 1 christopherreece staff 108 Dec 14 08:55 server_utils.sh
-rwxr-xr-x 1 christopherreece staff 23 Dec 14 09:04 short


contents of short



echo 'a short program'


contests of server_utils.sh



function upfile 
scp $1 root@myserveripadress:~/



Shell input and output.



$ hello
hello

$ short
-bash: short: command not found






share|improve this question






















  • Where did hello come from?
    – ilkkachu
    Dec 14 '17 at 0:43














up vote
3
down vote

favorite
1












I want to put a bunch of executable scripts in the .command dir (which is also executable), and then only have to source that directory in my .bash_profile. Is this possible? I can get this to work with one file. But when adding a second file, the second file's commands aren't available in the shell.



my .bashprofile



source ~/.commands/*


my .commands folder



-rwxr-xr-x 1 christopherreece staff 108 Dec 14 08:55 server_utils.sh
-rwxr-xr-x 1 christopherreece staff 23 Dec 14 09:04 short


contents of short



echo 'a short program'


contests of server_utils.sh



function upfile 
scp $1 root@myserveripadress:~/



Shell input and output.



$ hello
hello

$ short
-bash: short: command not found






share|improve this question






















  • Where did hello come from?
    – ilkkachu
    Dec 14 '17 at 0:43












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I want to put a bunch of executable scripts in the .command dir (which is also executable), and then only have to source that directory in my .bash_profile. Is this possible? I can get this to work with one file. But when adding a second file, the second file's commands aren't available in the shell.



my .bashprofile



source ~/.commands/*


my .commands folder



-rwxr-xr-x 1 christopherreece staff 108 Dec 14 08:55 server_utils.sh
-rwxr-xr-x 1 christopherreece staff 23 Dec 14 09:04 short


contents of short



echo 'a short program'


contests of server_utils.sh



function upfile 
scp $1 root@myserveripadress:~/



Shell input and output.



$ hello
hello

$ short
-bash: short: command not found






share|improve this question














I want to put a bunch of executable scripts in the .command dir (which is also executable), and then only have to source that directory in my .bash_profile. Is this possible? I can get this to work with one file. But when adding a second file, the second file's commands aren't available in the shell.



my .bashprofile



source ~/.commands/*


my .commands folder



-rwxr-xr-x 1 christopherreece staff 108 Dec 14 08:55 server_utils.sh
-rwxr-xr-x 1 christopherreece staff 23 Dec 14 09:04 short


contents of short



echo 'a short program'


contests of server_utils.sh



function upfile 
scp $1 root@myserveripadress:~/



Shell input and output.



$ hello
hello

$ short
-bash: short: command not found








share|improve this question













share|improve this question




share|improve this question








edited Dec 14 '17 at 0:34









ilkkachu

49.9k674137




49.9k674137










asked Dec 14 '17 at 0:20









Christopher Reece

182




182











  • Where did hello come from?
    – ilkkachu
    Dec 14 '17 at 0:43
















  • Where did hello come from?
    – ilkkachu
    Dec 14 '17 at 0:43















Where did hello come from?
– ilkkachu
Dec 14 '17 at 0:43




Where did hello come from?
– ilkkachu
Dec 14 '17 at 0:43










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










You can't do that with one source. The first argument is taken as the file name, the others show up as the positional parameters $1, $2... in the sourced script.



$ cat test.src 
echo hello $1
$ source test.src there
hello there


But you could do it with a loop:



for f in ~/commands/*.src; do
source "$f"
done


(As an aside, having stuff like that include only files with a certain extension is quite useful if you use an editor that leaves backup files with a trailing ~. The backup copies don't become accidentally active, then.)



Though note that if you have a sourced script that contains plain commands (like that echo above or your short), they'll be executed when the script is sourced. They don't generate any functions in the sourcing shell.



$ cat test2.src 
echo "shows when sourced"
func()
echo "shows when function used"

$ source test2.src
shows when sourced
$ func
shows when function used



If you want to have executable scripts instead, the kind where the script runs when you give its name as a command, put them somewhere in PATH (I'd suggest using ~/bin for that), give them the execute permission and put proper hashbangs in the beginning of the scripts (#!/bin/sh or #!/bin/bash or whatever)






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410773%2fcan-you-put-multiple-executable-scripts-in-one-directory-and-by-sourcing-that-d%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    You can't do that with one source. The first argument is taken as the file name, the others show up as the positional parameters $1, $2... in the sourced script.



    $ cat test.src 
    echo hello $1
    $ source test.src there
    hello there


    But you could do it with a loop:



    for f in ~/commands/*.src; do
    source "$f"
    done


    (As an aside, having stuff like that include only files with a certain extension is quite useful if you use an editor that leaves backup files with a trailing ~. The backup copies don't become accidentally active, then.)



    Though note that if you have a sourced script that contains plain commands (like that echo above or your short), they'll be executed when the script is sourced. They don't generate any functions in the sourcing shell.



    $ cat test2.src 
    echo "shows when sourced"
    func()
    echo "shows when function used"

    $ source test2.src
    shows when sourced
    $ func
    shows when function used



    If you want to have executable scripts instead, the kind where the script runs when you give its name as a command, put them somewhere in PATH (I'd suggest using ~/bin for that), give them the execute permission and put proper hashbangs in the beginning of the scripts (#!/bin/sh or #!/bin/bash or whatever)






    share|improve this answer


























      up vote
      4
      down vote



      accepted










      You can't do that with one source. The first argument is taken as the file name, the others show up as the positional parameters $1, $2... in the sourced script.



      $ cat test.src 
      echo hello $1
      $ source test.src there
      hello there


      But you could do it with a loop:



      for f in ~/commands/*.src; do
      source "$f"
      done


      (As an aside, having stuff like that include only files with a certain extension is quite useful if you use an editor that leaves backup files with a trailing ~. The backup copies don't become accidentally active, then.)



      Though note that if you have a sourced script that contains plain commands (like that echo above or your short), they'll be executed when the script is sourced. They don't generate any functions in the sourcing shell.



      $ cat test2.src 
      echo "shows when sourced"
      func()
      echo "shows when function used"

      $ source test2.src
      shows when sourced
      $ func
      shows when function used



      If you want to have executable scripts instead, the kind where the script runs when you give its name as a command, put them somewhere in PATH (I'd suggest using ~/bin for that), give them the execute permission and put proper hashbangs in the beginning of the scripts (#!/bin/sh or #!/bin/bash or whatever)






      share|improve this answer
























        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        You can't do that with one source. The first argument is taken as the file name, the others show up as the positional parameters $1, $2... in the sourced script.



        $ cat test.src 
        echo hello $1
        $ source test.src there
        hello there


        But you could do it with a loop:



        for f in ~/commands/*.src; do
        source "$f"
        done


        (As an aside, having stuff like that include only files with a certain extension is quite useful if you use an editor that leaves backup files with a trailing ~. The backup copies don't become accidentally active, then.)



        Though note that if you have a sourced script that contains plain commands (like that echo above or your short), they'll be executed when the script is sourced. They don't generate any functions in the sourcing shell.



        $ cat test2.src 
        echo "shows when sourced"
        func()
        echo "shows when function used"

        $ source test2.src
        shows when sourced
        $ func
        shows when function used



        If you want to have executable scripts instead, the kind where the script runs when you give its name as a command, put them somewhere in PATH (I'd suggest using ~/bin for that), give them the execute permission and put proper hashbangs in the beginning of the scripts (#!/bin/sh or #!/bin/bash or whatever)






        share|improve this answer














        You can't do that with one source. The first argument is taken as the file name, the others show up as the positional parameters $1, $2... in the sourced script.



        $ cat test.src 
        echo hello $1
        $ source test.src there
        hello there


        But you could do it with a loop:



        for f in ~/commands/*.src; do
        source "$f"
        done


        (As an aside, having stuff like that include only files with a certain extension is quite useful if you use an editor that leaves backup files with a trailing ~. The backup copies don't become accidentally active, then.)



        Though note that if you have a sourced script that contains plain commands (like that echo above or your short), they'll be executed when the script is sourced. They don't generate any functions in the sourcing shell.



        $ cat test2.src 
        echo "shows when sourced"
        func()
        echo "shows when function used"

        $ source test2.src
        shows when sourced
        $ func
        shows when function used



        If you want to have executable scripts instead, the kind where the script runs when you give its name as a command, put them somewhere in PATH (I'd suggest using ~/bin for that), give them the execute permission and put proper hashbangs in the beginning of the scripts (#!/bin/sh or #!/bin/bash or whatever)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 14 '17 at 0:51

























        answered Dec 14 '17 at 0:42









        ilkkachu

        49.9k674137




        49.9k674137






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f410773%2fcan-you-put-multiple-executable-scripts-in-one-directory-and-by-sourcing-that-d%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay