How to call the function of .bashrc correctly?

The following sentences are found in

.bashrc.

var="xxx"
fun1(){
    echo $var
}
export -f fun1

there was a problem calling the fun1 function in .bashrc from test.sh and could not get the value of var.
I rewrote

fun1(){
    var="xxx"
    echo $var
}
export -f fun1

you can call fun1 from test.sh.

there are two problems:
1.var= "xxx" needs to be called by other functions in .bashrc. Many people write a few more var= "xxx"
2.export-f fun1
if there are 10 functions that want to be called by other scripts from .bashrc, write 10 export-f funm?

.

how to solve these two problems properly?

Mar.03,2021

store shared variables and functions in a separate file and introduce it when you need to call it.

for example, here are the contents of the shared file common.source

author=""

function print_name() {
    echo "name: $1"
}

suppose you need to call foo () in the a.sh file, you can write

like this.
-sharp!/bin/bash
source common.source
print_name $author

question 1:
if you want to read $var, and export it in another script, you don't have to write it in the function.

question 2:
I think you mean ".bashrc has 10 functions that need to be called by other scripts". The answer is, you don't need to write 10 export, or even $var you don't use export. Just add set-a to the variables and functions that need export, and then the variable assignments and function definitions are automatically export.

-an Each variable or function that is created or modified is given the export attribute and marked for export to the environment of subsequent commands.
Menu