Parece que uma pergunta semelhante foi respondida aqui .
É claro que você pode usar .
ou source
para atribuir as variáveis se estiver confortável, mas também pode usar awk
ou grep
para executar apenas linhas específicas do arquivo que parece como declerations variáveis.
How to handle many variables
If you want to source all the variables assigned in
script1.sh
, consider:source <(grep -E '^\w+=' script1.sh)
This uses
grep
to extract all lines fromscript1.sh
that look like variable assignments. These lines are then run in the current shell, assigning the variables.If you use this approach first check to make sure that you want all the variables and that there aren't any that will interfere with what you are doing. If there are such, we can exclude them with a second
grep
command.Considering the pieces in turn:
source file
tells the shell to execute thefile
in the current shell.
<(...)
is called process substitution. It allows us to use the output of a command in place of a file name.The command
grep -E '^\w+=' script1.sh
extracts all lines that seem like variable assignments. If you run this command by itself on the command line, you should see something like:variable="Hello" var2="Value2"
and so on. You should do this first and inspect the output to make sure that these are the lines that you want to execute.