Can you put multiple executable scripts in one directory, and by sourcing that directory make all of those commands available?
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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
bash shell executable source
add a comment |Â
up vote
3
down vote
favorite
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
bash shell executable source
Where didhello
come from?
â ilkkachu
Dec 14 '17 at 0:43
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
bash shell executable source
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
bash shell executable source
edited Dec 14 '17 at 0:34
ilkkachu
49.9k674137
49.9k674137
asked Dec 14 '17 at 0:20
Christopher Reece
182
182
Where didhello
come from?
â ilkkachu
Dec 14 '17 at 0:43
add a comment |Â
Where didhello
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
add a comment |Â
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 source
d. 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)
add a comment |Â
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 source
d. 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)
add a comment |Â
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 source
d. 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)
add a comment |Â
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 source
d. 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)
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 source
d. 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)
edited Dec 14 '17 at 0:51
answered Dec 14 '17 at 0:42
ilkkachu
49.9k674137
49.9k674137
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Where did
hello
come from?â ilkkachu
Dec 14 '17 at 0:43