Thursday, May 2, 2024
22
rated 0 times [  22] [ 0]  / answers: 1 / hits: 37890  / 2 Years ago, sat, november 12, 2022, 10:38:04

I want to get a variable from another script, as demonstrated in this question on Stack Overflow:



How to reference a file for variables in a bash script



However, the answer uses the source command which is only available in bash. I want to do this in a portable way.



I have also tried



a.sh



export VAR="foo"
echo "executing a"


b.sh



#!/bin/sh
./a.sh
echo $VAR


But of course that does not work either. How to do this?


More From » command-line

 Answers
3

First of all, be aware that var and VAR are different variables.



To answer your question the . command is not bash-specific:



# a.sh
num=42




# b.sh
. ./a.sh
echo $num


The variables in "a" do not need to be exported.



http://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins


[#30777] Saturday, November 12, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tudatchful

Total Points: 270
Total Questions: 109
Total Answers: 122

Location: Palau
Member since Tue, May 30, 2023
1 Year ago
;