0

I have a text file with variables in format VARIABLE=value like:

PID=123123
WID=569569
TOKEN=456456456

Where VARIABLE has only uppercase letters and value contains only alphanumeric characters ([a-zA-Z0-9])

Now I want to pass these variables to a script as named arguments (options):

./script.sh --PID 123123 --WID 569569 --TOKEN 456456456

Is there a simple way to do this in bash?

I have read:

7
  • 1
    In the general case, xargs has problematic corner cases when the input file is large, but if it's just a few options, sed 's/^/--/;s/=/ /' file | xargs ./script.sh Commented Apr 11, 2023 at 7:59
  • @jonrsharpe I'm not asking about environment variables Commented Apr 11, 2023 at 8:04
  • 1
    Your question is unclear. You only give example definitions for variable, but not a precise definition of the syntax. Your example happens to look like bash variable definition, so if you would say that the text file's content simply is a piece of bash code, you could simply source the file using source FILENAME. This comes with all risks and perils of such an approach, i.e. as long as you are the one who prepares the "text file", you are safe, but if the file comes from a place outside of your control, you better do some parsing. Commented Apr 11, 2023 at 10:08
  • @user1934428 I added some constrains for the values to the question Commented Apr 11, 2023 at 10:25
  • That's great. You should now add that there are no spaces allowed in this line. If this is the case, you can think of it as bash code, and simply source the file. In this case, you need to add to what extent you want the file to be syntax-checked. Commented Apr 11, 2023 at 10:49

1 Answer 1

3

Read the file line by line as = separated elements and construct the array of arguments. Then call the script.

args=()
while IFS== read -r k v; do
   args+=("--$k" "$v")
done < yourtextfile.txt
./script.sh "${args[@]}"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.