How to get a variable value from the source file in Bash Script on Debian 12

To Get Variable Value From The Source File In Bash Script On Debian 12

In Bash scripting on Debian 12, obtaining variable values from a source file is a key technique for managing configuration settings or external parameters. This process involves integrating external files into scripts, enabling the script to access and utilize predefined variables.

Procedure Steps:

Step 1: Check the OS version by using following command.

root@linuxhelp:~/linuxhelp# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL=https://bugs.debian.org/

Step 2: Create a script file to assign value for the variable by using following command.

root@linuxhelp:~/linuxhelp# vim value
#!/bin/bash
VAR=$(which pwd)

Step 3: Make the executable permission by using following command.

root@linuxhelp:~/linuxhelp# chmod +x value

Step 4: Long list the file to check executable permission by using following command.

root@linuxhelp:~/linuxhelp# ls -la
total 12
drwxr-xr-x 2 root root 4096 Dec  8 05:18 .
drwx------ 8 root root 4096 Dec  8 05:18 ..
-rwxr-xr-x 1 root root   30 Dec  8 05:18 value

Step 5: Create a script file to get variable value from source file by using following command.

root@linuxhelp:~/linuxhelp# vim variable
#!/bin/bash

source ./value

VAR1=$(which pwd)

if [ $VAR != $VAR1 ]
then
	echo "The variable values are same, so copy the variable output!"
	cp $VAR /root/linuxhelp/output
else
	echo "The variable values are not same!"
fi

Step 6: Make the executable permission to the script file by using following command.

root@linuxhelp:~/linuxhelp# chmod +x variable

Step 7: Long list the file to check executable permission by using following command.

root@linuxhelp:~/linuxhelp# ls -la
total 16
drwxr-xr-x 2 root root 4096 Dec  8 05:21 .
drwx------ 8 root root 4096 Dec  8 05:21 ..
-rwxr-xr-x 1 root root   30 Dec  8 05:18 value
-rwxr-xr-x 1 root root  222 Dec  8 05:21 variable

Step 8: Run the script file by using following command.

root@linuxhelp:~/linuxhelp# ./variable 
The variable values are same, so copy the variable output!

Step 9: Long list the file to check the created file by using following command.

root@linuxhelp:~/linuxhelp# ls -la
total 60
drwxr-xr-x 2 root root  4096 Dec  8 05:24 .
drwx------ 8 root root  4096 Dec  8 05:23 ..
-rwxr-xr-x 1 root root 43952 Dec  8 05:24 output
-rwxr-xr-x 1 root root    30 Dec  8 05:18 value
-rwxr-xr-x 1 root root   222 Dec  8 05:23 variable

Step 10: Edit the script file to change condition by using following command.

root@linuxhelp:~/linuxhelp# vim variable
#!/bin/bash

source ./value

VAR1=$(which pwd)

if [ $VAR != $VAR1 ]
then
	echo "The variable values are same, so copy the variable output!"
	cp $VAR /root/linuxhelp/output
else
	echo "The variable values are not same!"
	rm -rf /root/linuxhelp/output
fi

Step 11: Run the script file by using following command.

root@linuxhelp:~/linuxhelp# ./variable 
The variable values are not same!

Step 12: Long list the files to check the removed file by using following command.

root@linuxhelp:~/linuxhelp# ls -la
total 16
drwxr-xr-x 2 root root 4096 Dec  8 05:27 .
drwx------ 8 root root 4096 Dec  8 05:27 ..
-rwxr-xr-x 1 root root   30 Dec  8 05:18 value
-rwxr-xr-x 1 root root  254 Dec  8 05:27 variable

Conclusion:

We have reached the end of this article. In this guide, we have walked you through the steps required to get variable value from the source file in Bash Script on Debian 12. Your feedback is much welcome.

FAQ
Q
What consideration should be taken regarding file paths?
A
Be mindful of file paths, using relative paths if the source file is in the same directory.
Q
Why is it essential to check the existence of the variable before using it?
A
Checking existence prevents errors and ensures the variable is defined.
Q
How do I access the variable value after sourcing the file?
A
Access the variable using the appropriate syntax, e.g., `$VARIABLE_NAME`.
Q
What should be ensured within the source file for successful variable retrieval?
A
The source file must contain the definition of the desired variable.
Q
What is the first step in obtaining a variable value from a source file?
A
Source the file using the `source` or `.` command.