Exporting Values of Variables to Another File

Hello,

I'm very new to Linux, and I have a question, I'm hoping you could help me with. :slight_smile:

I have created a file called subject, contains this code:

#!/bin/bash

read -p "Student Name: " NAME
read -p "Student ID: " ID
read -p "Address: " ADDRESS

I'm to create another file called data in the same directory and that file is supposed to import the variables from the subject and print their values.

#!/bin/bash

export NAME
export ID
export ADDRESS
./subject
echo "Name: $NAME"
echo "ID: $ID"
echo "Address: $ADDRESS"

However the above code doesn't import any value from the subject file, and just prints blank.

Thank you in advance :b:

Is this homework?

Nope, self learning. :slight_smile:

The 'data' file would just be

My name
My ID
My Address

and you'd use it like

./myscript < data

Thank you for you reply, but what I mean is how can I make the data use the values of the variables in subject and print them on the screen? Am I using the export incorrectly or is the problem somewhere else?

./subject < data

By redirecting data into ./subject with <, you are replacing it's standard input. read will read from the file, not the keyboard.

1 Like

You need to run the script called subject in a slightly different way such that it executes in the same shell as the script called data . It needs to be run as . ./subject (dot-space-dot-slash-scriptname).

#!/bin/bash

export NAME
export ID
export ADDRESS
. ./subject
echo "Name: $NAME"
echo "ID: $ID"
echo "Address: $ADDRESS"

This is the same way that your login profile works.
Btw. Calling a script data is very confusing and has made your earlier posts ambiguous.

1 Like

Thank you both! :slight_smile: