Shell Scripting is mainly used to automate taks with the help of shell scripting language, and Bash is the most used of em' `Bash` is the default shell in allmost all linux operating systems
This repo will contain complete tutorial about bash
- PC or Termux or ish
- a terminal
- bash or zsh installed
Shell Scripting is mainly used to automate taks with the help of shell scripting language and Bash is the most used of em' Bash is the default shell in allmost all linux operating systems
Type Following on any terminal
$ echo $BASH_VERSION
this will print current version of bash you are using
echo :
echo is same as printf in c programming or print in python programming the oly diffrence is more like the main diffrence is in its syntax , that it does not need a bracket before giving the strings as its arguments or the value that you want to display into the monitor/screen in this case$ :
"$" is used when when calling an object eg: consider the following examplein the above example we did assign a value to a newly initiated variable A , and we tried to print its value using echo but it is not printing the value of A that we initialy assigned
Now consider an another example
In this above example we got exactly we have expected ie, using echo we where able to print the value of A that we're initialy assigned
Notes:
You can get full usage of almost any command in linux usig --help option with_ _sometimes -h also works and do the same_ eg:> pwd --help
output
pwd: pwd [-LP]
Print the name of the current working directory. Options:
-L print the value of $PWD if it names the current working directory
-P print the physical directory, without any symbolic links
By default, `pwd' behaves as if `-L' were specified.
Exit Status:
Returns 0 unless an invalid option is given or the current directory
cannot be read
| Commands | Description |
|---|---|
mkdir |
To make a directories/directory(folder) |
ls |
List all the files in the current directry |
pwd |
Print the name of the current working directory |
cd |
change the current working directry |
mv |
move files to directories or rename files |
cp |
copy files to directories |
rm |
To remove files/directories |
mkdir (make directory) is used to create directory(folder) or miltiple directories (folders)
we can create a folder called new-directory using mkdie
$ mkdir new-folder
mkdiro list out folders and files the current working directory
we can also create multiple directories using mkdir
we can see that if we type more names after mkdir it creates mutliple directory
So the basic syntax of mkdir is
$ mkdir <foldername> <foldername2> ...
learn more about mkdir here
Normaly bash program will be situated under the /bin folder where almost all the user executable programs contains you can take a look at what inside that folder by
$ ls /bin
so we used ls to list all the files
Notes:
*we can give some arguments to the ls command which will produce diffrent outputs* *for example "-lh" argument combined with `ls` will show you the size of that file*-l is used to use a long listing format
-h is used to make it human readable
*Learn more about ls here
pwd is used to know your current directory
The /home/aruncs folder have an nother property it is yout home path more about this
cd is used for changing working directries(folder)
Notes
*To understand this simply consider if we have to delete a file which we have downloaded from internet and we dont know it's name but it is in the folder `$HOME/Downloads` we can do this in many ways azbut one of the easyest way is to go into the folder and inspect the files and delete the one that we dont need suppose we want to delete song.mp3 form download ; we first need to go to the folder specified above so inorder to go to that folder we use `cd`(change directory) command*cd /path/to/the/folder
we can see there is a folder named test in our current working directry
Tip: We used ls command to list(to see as a list) the files
Dont confuse with that path name, we will cover that one in linux file system
*So specified the path name after cd , after that we can see that our current directory is changed to /tmp/test/test before it was /tmp/test
cp is used to copy files/folders
cp old_name new_name
which renames file named name1 to name2
Note: We can use cp to copy folders as well as files
Explenation:
$ ls
song.mp3
$ cp song.mp3 old_song.mp3
$ ls
old_song.mp3
mv is mainly used to move or renames files the
mv old_name new_name
mv old_name new_name
Note: We can use mv to rename both folders and files
Explenation:
f
`rm` is used to remove files or folders,
rm file_name
rm folder_name
Note:
Inorder to remove folders/directories you need to use `--recursive` `-r` and you can also force the deletion using `-f` `--force` argument$ ls
song.mp3
$ rm song.mp3
$ ls
$
*We can check the current Varriables that are beeing used by just typing system
$ set
BASH=/usr/bin/bash
BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:histappend:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=([0]="0")
BASH_ARGV=()
BASH_CMDS=()
BASH_COMPLETION_VERSINFO=([0]="2" [1]="11")
BASH_LINENO=()
BASH_REMATCH=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="5" [1]="1" [2]="4" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='5.1.4(1)-release'
COLORTERM=truecolor
COLUMNS=127
COMP_WORDBREAKS=$' \t\n"\'><=;|&(:'
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
DESKTOP_SESSION=lightdm-xsession
DIRSTACK=()
DISPLAY=:0.0
EUID=1000
GDMSESSION=lightdm-xsession
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
GROUPS=()
GTK_MODULES=gail:atk-bridge
GTK_OVERLAY_SCROLLING=0
HISTCONTROL=ignoreboth
HISTFILE=/home/axux/.bash_history
HISTFILESIZE=2000
HISTSIZE=1000
HOME=/home/axux
HOSTNAME=parrot
HOSTTYPE=x86_64
IFS=$' \t\n'
HOME
home path will be always be your /home/username unless you cange it
Also ~ this symbol is linked to HOME variable/reference
An important thing to note is that you have make scrip files as executable by using chmod by chmod +x filename.sh
you can use either echo or printf to ptint a string
$ echo "Hello World"
Hello World!
$ printf "Hello World!"
Hello World!
commands used : echo printf
We use read to read Users input
read.sh
echo "Enter your Name"
read name
echo "Hi $name"
bash read.sh
Explenation:
#!/bin/bash is called the shebang which tells the computer to run the script using the bash shell.
echo command is used to print
read command is used to get user input
- Sum of 2 Numbers
echo $((num1 + num2))
You can either specify num1 and num2 or get it from user on any other programm
ie:
$ num1=10
$ num2=20
$ echo $((num1 + num2))
30
#!/bin/bash
echo "Enter the first Number"
read number1
echo "Enter the second Number"
read number2
echo "sum = " $((number1 + number2))
- Multiplication of Two numbers
consider we initialize num as 0 the we use `((num+=1))``
$ num=0
$ echo $n
0
$ ((num+=1))
$echo $n
1
$ ((numbers+=1))
$ echo $n
2













