02 Variables - kumar159man/MyShellLearning GitHub Wiki

Variables

Naming a variable

  • Variable name should start from a-z or A-Z followed by _ or digits 0-9
  • x=13 is the proper way to declare variable

Storing integer in a variable

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ x=5
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo "$x"
5

Storing String in a variable

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ x=SHELL
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo "$x"
SHELL
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ x="Bash Shell Scripting"
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo "$x"
Bash Shell Scripting

Storing float in a variable

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ x=10.23
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo "$x"
10.23

Storing output of a command in a variable

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ x=$(ls)
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo "$x"
Dockerfile
dockerfileforCustomNginx
err.txt
nodeApp
output.txt
StaticWebsite
t1.txt
t2.txt

Basics of shell script

#!/usr/bin/bash
echo "Hello this is my first shell script" 

First line (shebang) consist of path consisting path of bash shell . This is must for every shell script. Executing this shell script require execute permission.

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls -l
total 4
-rw-rw-r-- 1 myubuntu myubuntu 62 Jul  1 20:52 first.sh
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ vi first.sh 
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls -l
total 4
-rw-rw-r-- 1 myubuntu myubuntu 60 Jul  1 20:56 first.sh
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ chmod +x first.sh 
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls -l
total 4
-rwxrwxr-x 1 myubuntu myubuntu 60 Jul  1 20:56 first.sh

Once execute permission is given then in order to run issue command ./{shellScriptName}

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./first.sh 
Hello this is my first shell script

Running shell script from another shell script

Lets create second.sh shell script and invoke first.sh from it

#!/usr/bin/bash
./first.sh

./{path of the shell script} should be placed in the shell script. Now Let's run

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./second.sh 
Hello this is my first shell script

Accessing variables from other shell script or file

Lets create a file and have variables x and y

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat variables.txt 
x=18
y=20

Also let's create another variable in a shell script

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat first.sh 
#!/usr/bin/bash
echo "Hello this is my first shell script"
dog="Chelsea" 

Content of shell script where we are accesing variables from a file

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat third.sh 
#!/usr/bin/bash
source ./variables.txt
. ./first.sh
echo "Value of x is $x"
echo "Value of y is $y"
echo "Value of dog is $dog"

source and . is used to access the variable in the shell script from another file Output

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./third.sh
Hello this is my first shell script
Value of x is 18
Value of y is 20
Value of dog is Chelsea

Storing exit status in a variable

In Linux execution of command returns an exit code . exit code ranges from 0-255. Through $?we can get exit status

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls
first.sh  second.sh  third.sh  variables.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo $?
0
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ lddf

Command 'lddf' not found, did you mean:

  command 'ldd' from deb libc-bin (2.31-0ubuntu9)
  command 'ldrdf' from deb nasm (2.14.02-1)

Try: sudo apt install <deb name>

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo $?
127
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls -ltr
total 16
-rwxrwxr-x 1 myubuntu myubuntu  27 Jul  1 21:01 second.sh
-rw-rw-r-- 1 myubuntu myubuntu  10 Jul  1 21:08 variables.txt
-rwxrwxr-x 1 myubuntu myubuntu  74 Jul  1 21:13 first.sh
-rwxrwxr-x 1 myubuntu myubuntu 128 Jul  1 21:17 third.sh
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo $?
0
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls -xyz
ls: invalid option -- 'y'
Try 'ls --help' for more information.
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo $?
2
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ls
first.sh  second.sh  third.sh  variables.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ exit_status=$?
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ echo "$exit_status"
0
⚠️ **GitHub.com Fallback** ⚠️