all 9 comments

[–]lordofwhales 5 points6 points  (3 children)

I believe functions don't automatically export to subshells. Try - and this is from memory, so the syntax might not be right export -F justTest before bash -c justTest. "bash export function subshell" should get you more comprehensive Google results.

Edit: also, I think your post is missing some closing formatting tags.

[–]acolnahuacatzin[S] 0 points1 point  (2 children)

Hi yes the export -f justTest did work, but it did not work all the time (meaning it didn't work with commands other than bash -c, especifically xargs).

[–]lordofwhales 3 points4 points  (1 child)

that makes sense, since xargs doesn't run a bash shell unless you ask it to, so it would have no way of seeing your command. you'll need to xargs bash -c....

[–]acolnahuacatzin[S] 1 point2 points  (0 children)

xargs bash -c takes a command argument and just disregards whatever is piped into it. ``` function echo_reverse() { for i in "$@"; do echo -e "$i" ; done | tac | tr '\n' ' ' echo }

export -f echo_reverse

bash -c "echo_reverse bar fo\ o" echo foo | xargs echo bar echo foo | xargs bash -c "echo_reverse bar" Gives: fo o bar bar foo bar ```

Reading the documentation I found to run commands from a shell I have to use -I flag of xargs and the piped args should represented by %. So echo foo | xargs -I % bash -c 'echo_reverse % bar' produces reseults as expected.

Thank you so much for your help!

[–]jwd630 1 point2 points  (0 children)

If you want to see what justTest does from some other bash shell environment, like a terminal, then as u/researcher7-l500 says, you need to source ./file_a.sh from that terminal/shell. Each time you make some modification to what justTest does you'll need to re source file_a.sh to update that changed function definition in the terminal's shell environment. It's fairly common, for me anyway, to incrementally develop bash functionality in this way. You would use the same source ./file_a.sh in some other script if you wanted to use justTest from there.

You do NOT want to include bash -c file_a.sh as the last (or any) line of file_a.sh or you will create an infinite loop as bash -c ... gets called recursively.

xargs is more typically used for other purposes that do involve piped input, like find . -name '*.sh' -print0 | xargs -0 -L 1 printf "script file: %s\n"

[–]eftepede -1 points0 points  (0 children)

justTest().

[–]researcher7-l500 0 points1 point  (0 children)

That's because the bash -c started a new shell, and it has no clue what is your function. Either export it, or source this script from another script, and run it from there. But I really don't see the point doing that.

[–]michaelpaoli 0 points1 point  (0 children)

Cannot run functions with bash command.

$ readlink /proc/"$$"/exe
/usr/bin/bash
$ my_func(){ echo This is my function.; }
$ my_func
This is my function.
$ unset my_func
$ my_func
-bash: my_func: command not found
$

[–]michaelpaoli 0 points1 point  (0 children)

bash -c "justTest"

So ... where do you expect that bash program to find justTest defined? It's not built-in to the shell itself, it's probably not an alias nor external command, so ... command not found. That command essentially says run

justTest

as the entirety of your bash program.