I'm very new to Linux, and I have a question, I'm hoping you could help me with.
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 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?
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.