Linux Bash Scripting Tutorial


Introduction

what is bash?

Bash is the shell or command line interpreter, Bash is acronyum for Born Again SHell authored by Stephen Bourne Bash is largely compatabile with sh and incorporates usefull features in korn shell ksh and C-shell csh Bash is portable.it currently runs on every version of UNIX system and few other operating systems. OS/2, And Windows Operating System


What is Shell?
Shell is both Command interpreter and a programming language. As a command interpreter, shell provides the user interface to the rich set of GNU utilities. The programming feature allows these commands can be combined. Shells may be used interactive or non-interactively. In interactive mode shells accpet input from terminal when executing commands in non-interactive mode shells accept commands from a file Shells allows execution of commands in synchronously and asynchronously. The shell waits for synchornous commands to complete before accepting more input. asynchronous commands continues to execute with parellel with shell The power of shell due to embedded programming languages.Like High-level programming langauges , the shell provides variables, flow-controls , functions etc.,

Writting first bash script

steps required to write first bash scripting program

  • Create a file using favourite editor, ex: vi,ed,nano,gedit or using touch command, usually all files end with .sh extenstion,its optional
  • First statement should be #!/usr/bin/bash ,the script is going to use bash binary to execute rest of the commands in a script file
  • write sequence commands,functions etc.,
  • save the file
  • Give Executable permission using chmod command
  • Execute the script file. ex: $ ~> bash scriptfile


Writting First Script: script.sh

step1:
		$ ~> touch script.sh
	
step2: Open the file using favourite editor.
		$ ~> vim script.sh
	
step3: first statement should be type of shell/interpreter you are going to use to execute the script.Here we are using bash shell,specify path of the bash executable
#!/usr/bin/bash , it should start with hash symbol followed by exclamation symbol then path of the executable. Bash imposes this type of syntax.Programmer must follow. #!/usr/bin/bash this is also called as "She-Bang"
	#!/usr/bin/bash
	
step4:write sequence of commands,functions etc., The following code 4 commands, echo command outputs its content,uname command find system information, cat command displays file content /etc/fedora-release.
		echo "Hello World!"
		echo "Welcome to Bash Scripting."

		uname -s
		cat /etc/fedora-release		
	

Here is complete script file script.sh

	#!/usr/bin/bash
		echo "Hello World!"
		echo "Welcome to Bash Scripting."

		uname -s
		cat /etc/fedora-release		

	
step5: give executable permissions using chmod command.
		$ ~> chmod +x script.sh
	
step6: executable the script file.
		$ ~> bash script.sh
		 or
		$ ~> ./script.sh
	
output:
Hello World!
Welcome to Bash Scripting.
Linux
Fedora release 30 (Thirty)

	

Bash Startup Files

/etc/profile

/etc/bashrc

       Systemwide functions and aliases for bash

/$HOME/.bash_profile

/$HOME/.bashrc



logout file

/$HOME/.bash_logout

ADS