01 Bash Shell scripting - kumar159man/MyShellLearning GitHub Wiki

BASH shell scripting

Which shell is there in system

echo $SHELL

myubuntu@myubuntu-VirtualBox:~$ echo $SHELL
/bin/bash

Shells available on the system

myubuntu@myubuntu-VirtualBox:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash

How to get path of Bash

which bash

myubuntu@myubuntu-VirtualBox:~$ which bash
/usr/bin/bash

Input output and error re directors

0 -> input redirection 1 -> output redirection 2 -> error redirection

Input redirection

< is used for input redirection

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ cat < Dockerfile 
FROM ubuntu
LABEL myfirst docker file
RUN apt-get update
RUN apt-get install -y nginx
CMD ["echo","Image has been created"]

Output redirection

  • > is used to write in a file
  • >> is used to append on an existing file
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ ls > output.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ cat output.txt 
Dockerfile
dockerfileforCustomNginx
nodeApp
output.txt
StaticWebsite

Using 1 to write in a file

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ ls 1>t1.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ cat t1.txt 
Dockerfile
dockerfileforCustomNginx
nodeApp
output.txt
StaticWebsite
t1.txt

Error redirection

Use 2 to redirect the error out put

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ csf 2>err.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ cat err.txt 

Command 'csf' not found, did you mean:

  command 'qsf' from deb qsf (1.2.7-1.3build3)
  command 'sf' from deb ruby-sprite-factory (1.7.1-3)
  command 'csh' from deb csh (20110502-5)
  command 'csh' from deb tcsh (6.21.00-1)
  command 'csc' from deb chicken-bin (5.1.0-1)
  command 'gsf' from deb libgsf-bin (1.14.46-1)
  command 'cst' from deb imx-code-signing-tool (3.3.0+dfsg-2)
  command 'csr' from deb rheolef (7.1-1)
  command 'cs' from deb csound (1:6.13.0~dfsg-3build2)
  command 'psf' from deb tcm (2.20+TSQD-5build1)
  command 'csi' from deb chicken-bin (5.1.0-1)

Try: sudo apt install <deb name>

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ 

Writing stdout and stderr in the same file

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ ls &>t2.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ cat t2.txt 
Dockerfile
dockerfileforCustomNginx
err.txt
nodeApp
output.txt
StaticWebsite
t1.txt
t2.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ hfdsa &>t2.txt
myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ cat t2.txt
hfdsa: command not found

ECHO command

echo is used to display/print in shell scripting

echo for displaying a string

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo "Hello"
Hello

echo for running command

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

echo with variable

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

echo with -n flag

n flag will not take the pointer in the new line

myubuntu@myubuntu-VirtualBox:~$ echo "Hello";echo "World"
Hello
World
myubuntu@myubuntu-VirtualBox:~$ echo -n "Hello";echo "World"
HelloWorld

echo with -e flag

-e flag is used if we have \n,\b , \t etc

myubuntu@myubuntu-VirtualBox:~/Desktop/docKerFile$ echo -e "Hello this is first line\nThis is second line"
Hello this is first line
This is second line
⚠️ **GitHub.com Fallback** ⚠️