Using Variables in Bash. What is $ sign in bash ? How to set shell variables ?
It is not essential to declare the type of the variable, as is the case in other programming languages, and it is accepted as a string in the text file rather than an integer, similar to java, go, c etc.
Correctly assigned variable: my_num1, _var1, myNum2
Incorrect variable assign: my_num1!, num1.num2, num1@num2, num2-num3, 4_number
When setting, there shouldn’t be a space between the name=variable sign and the variable.
There are just integers and not floating point numbers supported in bash:
ubuntu:~/Bash_scripts# num1=2.5 num2=4.5 num3=2 num4=5
ubuntu:~/Bash_scripts# echo $num1 $num2 $num3 $num4
2.5 4.5 2 5
ubuntu:~/Bash_scripts# echo $((num1 + num2))
-bash: 2.5: syntax error: invalid arithmetic operator (error token is ".5")
ubuntu:~/Bash_scripts# echo `expr $num3 + $num4`
7
root@ubuntu:~/Bash_scripts# echo $((num3 + num4))
7
$ sign inside the script or bash shell call variable name:
ubuntu:~# echo "First number num1 and second is num2"
First number num1 and second is num2
ubuntu:~# echo "First number $num1 and second is $num2"
First number 2.5 and second is 4.5
ubuntu:~# echo 'First number $num1 and second is $num2'
First number $num1 and second is $num2
ubuntu:~# echo First number \$num1 and second is $num2
First number $num1 and second is 4.5
ubuntu:~# echo "First number \$num1 and second is $num2"
First number $num1 and second is 4.5
ubuntu:~# echo 'First number \$num1 and second is $num2'
First number \$num1 and second is $num2
unset useful command to remove declared values:
ubuntu:~/Bash_scripts# COLOR=red
ubuntu:~/Bash_scripts# set | grep COLOR
COLOR=red
...
ubuntu:~/Bash_scripts# unset COLOR
ubuntu:~/Bash_scripts# set | grep COLOR
$$ command shows PID of current shell:
ubuntu:~/Bash_scripts# echo $$
7977
root@ubuntu:~/Bash_scripts# ps
PID TTY TIME CMD
7977 pts/1 00:00:00 bash
$n parameter $9 possible get user inputs:
ubuntu:~/Bash_scripts# vim bash_variables.sh
1 #!/bin/bash
2 echo "Inputs: " $1 $2 $3 $4 $5 $6 $7 $8 $9
ubuntu:~/Bash_scripts# bash bash_variables.sh 1 2 3 4 5 6 7 8 9
Inputs: 1 2 3 4 5 6 7 8 9 10
$# total number of outputs:
ubuntu:~/Bash_scripts# vim bash_variables.sh
1 #!/bin/bash
2 echo "Inputs: " $1 $2 $3 $4 $5 $6 $7 $8 $9
3 echo "Total: " $#
ubuntu:~/Bash_scripts# bash bash_variables.sh 1 2 3 4
Inputs: 1 2 3 4
Total: 4
$* for shows $1-$9 everything of inputs:
ubuntu:~/Bash_scripts# vim bash_variables.sh
1 #!/bin/bash
2 echo "Inputs: " $*
3 echo "Total: " $#
ubuntu:~/Bash_scripts# bash bash_variables.sh 1 2 3 4 5 6
Inputs: 1 2 3 4 5 6
Total: 6
$? status of last executed command :
ubuntu:~/Bash_scripts# echo $?
0
$! PID of last background command:
ubuntu:~/Bash_scripts# sleep 30 &
[1] 8656
root@ubuntu:~/Bash_scripts# echo $!
8656
$0 also shows which shell we are in
ubuntu:~/Bash_scripts# echo $0
-bash
ubuntu:~/Bash_scripts# zsh
ubuntu# echo $0
zsh
CAPITAL CASE some bash commands:
ubuntu:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:
/usr/local/games:/snap/bin
ubuntu:~# echo $USER
root
ubuntu:~# echo $UID
0
ubuntu:~# echo $HOME
/root
ubuntu:~# echo $HISTFILE
/root/.bash_history
ubuntu:~# echo $HISTSIZE
1000
Finally, declare variable no way to remove and change:
root@ubuntu:~# declare -r num1=2 num2=5
root@ubuntu:~# echo $num1 $num2
2 5
ubuntu:~/Bash_scripts# num1=14
-bash: num1: readonly variable
ubuntu:~/Bash_scripts# unset num1
-bash: unset: num1: cannot unset: readonly variable
Conclusion
The three types of variables in bash are this environment, shell, and local variables. Keep in touch environment variable will be next.