This Topic is about Linux Admin – Variables.
Variables in Bash are used like any other scripting language. The syntax may vary from languages such as Perl, Python and Ruby.
The first thing we will want to note is BASH variables comes in two basic varieties: Global or Environment Variables and Script or Local Variables.
Global or Environment Variables are set across all shells and scripts. Environment Variables can be displayed with either the env or printenv commands −
bash-3.2# printenv SHELL=/bin/bash TERM=xterm-256color USER=root SUDO_USER="rick cardon" SUDO_UID=501 USERNAME=root MAIL=/var/mail/root PATH=/usr/local/pear/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin: /opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/ PWD=/home/rdc/Desktop LANG=en_US.UTF-8
Common syntax is to use uppercase for Environment variables and lowercase script for local variables. These are the variables used within your script, assuming the script will not be setting or changing the shell Environment Variables.
Script or Local Variables are only accessible to the current shell.
#!/bin/bash num = 0 while [ $num -lt 100 ] do num = $[$num+1] echo $num if [ $((num % 10)) = 0 ]; then sleep 5s fi done echo $PATH
The small script above simply increments to 20, pauses for five seconds every 5th iteration, then echoes an environmental variable: the current user’s path −
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /usr/local/pear/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/opt/ local/bin:/opt/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
If you’d like a variable to be seen outside your shell, the following export command must be used.
#!/bin/bash export MY_NEW_GLOBAL = "I am Global"
In this topic we learned about Linux Admin – Variables . To know more. Click Here .
Pingback: Linux Admin - Basic Math Operations - Adglob Infosystem Pvt Ltd
Pingback: Linux Admin - Shell Scripting - Adglob Infosystem Pvt Ltd