• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

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

  • 00:44 cat /etc/os-release
  • 01:01 vim value
  • 01:50 chmod +x value
  • 02:00 ls -la
  • 02:14 vim variable
  • 04:07 chmod +x variable
  • 04:19 ls -la
  • 04:26 ./variable
  • 04:44 ls -la
  • 04:59 vim variable
  • 05:41 ./variable
  • 05:54 ls -la
{{postValue.id}}

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.

Tags:
gabriel
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

What is the first step in obtaining a variable value from a source file?

A

Source the file using the `source` or `.` command.

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

How do I access the variable value after sourcing the file?

A

Access the variable using the appropriate syntax, e.g., `$VARIABLE_NAME`.

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

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.

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Ryan ?
how to use visual traceroute tool

Am using traceroute command to check for the route. i got this tool while surfing. So pls help me out installation and usage of Visual traceroute tool.

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.