Include a bash function into the parent script
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I can to define function in bash and use it:
foo() echo $1;
foo test
But if I want to collect my functions in one bash script all its unavailable:
init.bash
#!/bin/bash
foo() echo $1;
export -f foo # This not helps
Using:
./init.bash && foo test # error here
Is there any way to export script's functions to parent scope?
Without writing to .bashrc
, it's too global
Same as .bashrc
but for current bash instance only...
bash shell function
add a comment |Â
up vote
1
down vote
favorite
I can to define function in bash and use it:
foo() echo $1;
foo test
But if I want to collect my functions in one bash script all its unavailable:
init.bash
#!/bin/bash
foo() echo $1;
export -f foo # This not helps
Using:
./init.bash && foo test # error here
Is there any way to export script's functions to parent scope?
Without writing to .bashrc
, it's too global
Same as .bashrc
but for current bash instance only...
bash shell function
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I can to define function in bash and use it:
foo() echo $1;
foo test
But if I want to collect my functions in one bash script all its unavailable:
init.bash
#!/bin/bash
foo() echo $1;
export -f foo # This not helps
Using:
./init.bash && foo test # error here
Is there any way to export script's functions to parent scope?
Without writing to .bashrc
, it's too global
Same as .bashrc
but for current bash instance only...
bash shell function
I can to define function in bash and use it:
foo() echo $1;
foo test
But if I want to collect my functions in one bash script all its unavailable:
init.bash
#!/bin/bash
foo() echo $1;
export -f foo # This not helps
Using:
./init.bash && foo test # error here
Is there any way to export script's functions to parent scope?
Without writing to .bashrc
, it's too global
Same as .bashrc
but for current bash instance only...
bash shell function
bash shell function
edited 11 mins ago
ivan_pozdeev
35219
35219
asked May 3 '14 at 15:56
vp_arth
1084
1084
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
You could source
the file init.sh
. No need to export
the function in that file.
$ cat init.bash
foo() echo $1;
And use it:
$ . ./init.bash && foo test
test
Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.
export
would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.
Exactly, what I looking for. Thanks
â vp_arth
May 3 '14 at 16:07
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
You could source
the file init.sh
. No need to export
the function in that file.
$ cat init.bash
foo() echo $1;
And use it:
$ . ./init.bash && foo test
test
Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.
export
would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.
Exactly, what I looking for. Thanks
â vp_arth
May 3 '14 at 16:07
add a comment |Â
up vote
3
down vote
accepted
You could source
the file init.sh
. No need to export
the function in that file.
$ cat init.bash
foo() echo $1;
And use it:
$ . ./init.bash && foo test
test
Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.
export
would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.
Exactly, what I looking for. Thanks
â vp_arth
May 3 '14 at 16:07
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You could source
the file init.sh
. No need to export
the function in that file.
$ cat init.bash
foo() echo $1;
And use it:
$ . ./init.bash && foo test
test
Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.
export
would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.
You could source
the file init.sh
. No need to export
the function in that file.
$ cat init.bash
foo() echo $1;
And use it:
$ . ./init.bash && foo test
test
Sourcing a file would execute commands from it in the current shell context. As such, the functions would be available in the parent.
export
would set the attribute for a variable that would be applicable for the current shell and subshells. Not the parent shell. You need to define the variable in the current shell context.
answered May 3 '14 at 15:59
devnull
8,47112842
8,47112842
Exactly, what I looking for. Thanks
â vp_arth
May 3 '14 at 16:07
add a comment |Â
Exactly, what I looking for. Thanks
â vp_arth
May 3 '14 at 16:07
Exactly, what I looking for. Thanks
â vp_arth
May 3 '14 at 16:07
Exactly, what I looking for. Thanks
â vp_arth
May 3 '14 at 16:07
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%2f127700%2finclude-a-bash-function-into-the-parent-script%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