Difference between sh and bash 3
Udemy course Content starts from here 4
Extraction of the string & count characters of the variables 8
Permanent Variable inside Login script 9
Permissions in the terminal 14
Files’s relation with permissions 14
Run Bash script from terminal 15
Variable scope in functions 20
Redirect input : here document 22
Redirect input : here string 22
Link: https://www.geeksforgeeks.org/bash-script-difference-between-bash-script-and-shell-script/
In computer programming, a script is defined as a sequence of instructions that is executed by another program.
Command [options] [arguments]
Every command has a name
Command can have options (it’s like flag -a, -h, -iv).
Commands can have arguments
Ex; command = ls (list all content of the directory)
Commands | Function |
pwd cd ~ | gives current directory Home directory |
ls // access particular folder
| 1. List of items in the folder 2. Content of the folder 3. Gives you list in long format
4. To know which one is executable and which one is a folder
5. Information about folder // time |
# | comments |
Clear | Clear your screen |
cd folderName | Change directory |
Command | Function |
Relative path | Relative path because it is relative to where you are right now. Ex; if you are at /gautampambhar/work/ cd Pace/shared // this is the path is relative to where you are right now It does not start with root(/). It always starts with folder name |
Absolute path | Always starts with the root folder path (/). Ex; cd /user/gautampambhar/work/PACE |
cd .. | Back one folder |
cd - | It will take you where you were before starting to use cd command |
TAB | It will complete the incomplete folder name |
ls -a ls -af | To see hidden files To see hidden folders |
Commands | Function |
ls . | Gives list of items in current which is similar to ls command |
|
|
cp ../demo.zip . | Copy file(in the parent directory) to present directory |
cp -r folder1 folder2 | Copies one folder(folder1) inside another folder(folder2) -r stands for copy(recursively) everything inside folder1 into folder2 |
Commands | Function |
|
|
mv -v play/demo.txt . | Moves the file from play folder into current directory |
mv -v folder1 folder2 | Moves one folder into another // don’t require recursive -r in moving folders |
mv demo NewName.txt | Rename file from demo to NewName.txt |
Commands | Function |
cat gautam | Print file content |
cat -n gautam | Prints all the line(in the file) in number |
cat gautam newfile | It concatenates the file content of newfile into gautam file |
more gautam | If a gautam file has too many lines you can use the “more” command which always starts printing content from the top. And you can go down by down arrow and quit the terminal by entering Q |
less gautam | It opens a file content in a separate window. No content will be shown in the same terminal unlike More and cat commands |
/ | Type searchable(pattern) text and by “N” you can go to the next word and by “B” you can go to previous word |
To print outputs in a different way
Commands | Function |
echo | Print whatever you write inside parentheses |
|
Hi There
Hi There |
echo -e “hi \v there” | Print the second word using tab space. \v(stands for vertical tab) o/p: Hi there Which is similar to echo -e “hello \n \t there” |
Commands | Function |
touch filename | To create a file |
|
|
|
|
Commands | Function |
* | Any character in the file name Usage:
|
ls ??.txt ls ???.txt | Any single character Usage: List all the files that have only 2 characters: ls ??.txt |
ls [abc]*.txt | List all the files that starts with either a,b or c character o/p: afile.txt, bob.txt, cat.txt |
ls [abc][ab].txt | Combination of characters. First character has to be either a,b or c and second character has to be a or b o/p: ab.txt |
[!abc] | To exclude character. ! stands for bang Any character that shouldn’t have a,b,c Usage: ls [!abc].txt // will output file that doesn’t have a starting character from a,b,c |
ls [[:alpha:]]*.txt | Lists files that starts with letter in the file name |
ls [[:digit:]]*.txt | Lists files that starts with digits in the file name |
ls [[:lower/upper:]]*.txt | Lists files that starts with lower/upper case character in the file name |
Commands | Function |
bob=103 | To declare variable and it’s value |
declare -i num=10 | #Declare variable with integer type |
|
|
c=”this is string” | Use “” to assign string value to variable |
unset c | Unset the value of variable |
${#c} | TO know the length of value(how many character in the value) o/p: 14 |
{a:4:5}
1st character denotes the character you want to start from // always starts with position 0
2nd character denotes the length you want from the 1st character’s output // always starts with position 1 // total length of the value
Commands | Function |
a=0987654 ${a:4} | Gives output start from 4th character // cut character from 1 to 4 o/p: 654 |
b=”hello there” ${b:4:5} | Gives output from 4th character and takes only 3 character from that output o/p: o the |
a=123456789 ${a: -3} | To list the last 3 characters. // space is required // starts with 1 position o/p: 789 // {a:6} = {a: -3} |
${a: -3:2} | To list length of 1st character’s output o/p: 78 |
a=”hello there”
b=123456789
Commands | Function |
${a#h} | To remove character from the starting point use #. If you want to remove character that doesn’t there it doesn’t affect the output: ${a#w} // still prints hello there |
${a#hell} ${b#123} | o/p: o there. o/p: 456789 |
${a#*t} ${b#*7} | Remove everything that starts with something and arrive till t o/p: here o/p: 89 |
${b%789} | If you want to remove something from the end of the string or character use %. o/p: 123456 |
${b%3*} ${a%l*} | o/p: 12 o/p: hel |
${a%ere} | o/p: hello th |
You can store permanent variables inside the login script.
You can find this login script in .profile, .bashrc, .login files. And there you can add or change existing variable sets in this file. So you don’t need to set that variable every time you open the terminal.
Read command takes input and uses a variable to store it
Commands | Function |
read a 12345 | Read command takes input and uses a variable to store it o/p: echo $a // 12345 |
read -p “give me the value of the variable: ” q Give me the vale of the variable: 12345 | You can put the message to input the value o/p: echo $q // 12345 |
read PAPA | If you don’t give anything to read command and want to access value. This variable will always put inside $REPLY $REPLY // PAPA |
How to redirect output of the command to whatever file we want(and not just to terminal)
Commands | Function |
ls -l 1> fileName | Redirect output to some file name you provide // you don’t need 1 every time. |
ls -l 1>> fileName | Append text to the file. So existing file content stays as it is |
cat file1 file 2 1> file3 | Append content of 2nd file and create a new file |
Commands | Function |
ls -l file1 filejwdnjq 2> file2 | To redirect error message to the file o/p: filejwdnjq is not found // this output will be directed in the file2 & file 1 will be shown in the terminal |
ls -l file1 filejwdnjq &> file2 | To redirect both error message and standard message to a file |
ls -l file1 filejwdnjq 1> output.txt 2> error.txt | To redirect standards output to one file and error to different file |
ls -l file1 filejwdnjq 2> /dev/null | To redirect error output to null directory. Why? - bcz some error messages are not important to us to show. In that case we can take out the error message and put it in the another directory |
Commands | Function |
ls folder1 | less | Takes first command output and use it as an input of an another command o/p: Folder1 File1 Folder2 Tst.txt (end) |
tail -3 file.txt | Outputs last 3 line of that file |
ls folder1 | tail -3 | Outputs last 3 folder name o/p: File1 Folder2 Tst.txt |
ls folder1 | tail -3 | less | o/p: File1 Folder2 Tst.txt (end) |
ls folder1 | tail -3 | sort | Sort output alphabetically o/p: Abc.txt Bcd.txt cde.txt |
ls folder1 | tail -3 | sort > file.txt | Redirect output in some file |
Commands | Function |
Grep some filename.txt | Use grep command To find something inside the file. Basically will output the line that contains “some” This is case sensitive o/p: There is something |
Grep -i some filename.txt | Avoid case-sensitivity on your findings o/p: There is something Son |
ls | grep g | Grep with pipe command to list all files that has “g” character in it |
ls | grep -v g | It looks everything except line or file contains “g” in it |
Commands | Function |
echo {1,2,3} echo {1,2,3}_file | Brace expansion works with numbers and letters o/p: 1 2 3 1_file 2_file 3_file |
echo example{1..4} | o/p: Example1 Example2 Example3 Example4 |
Touch file{1..4} Mkdir folder{1..4} | Creates file1 file2 file3 file4 files/folders |
echo {a,b,c}_{1,2,3} | Create the combination of 2 braces o/p: A_1 a_2 a_3 b_1 b_2 b_3 c_1 c_2 c_3 |
In the pic below every files belongs to gautampambhar is the “USER/OWNER” and staff is the “GROUP”
Commands | Function |
whoami | Gives you user name of the user(terminal) |
Commands | Function |
Chmod u=rw file.txt | Change the file permission to user/owner |
Chmod g=rw file.txt | Change the file permission to group |
Chmod o=rw file.txt | Change the file permission to “Other” users |
Chmod +x file.txt | Give everyone's execute permission (user/group/others) |
Chmod -x file.txt | Take out everyone's execute permission (user/group/others) |
#! /bin/bash = telling your program that you want to interpret everything you find in this file; read as a bash. = telling the system that this program is bash and run in the bash shell
Commands | Function |
./filename | To run bash script from the terminal |
If → else condition // to check one condition
If → elif(n) → else condition // to check multiple condition
Condition can be grouped in 3 ways
If → strings
If → numbers
If → files
Commands | Function |
If [ “$d” == “hello” ] | Strings equals to hello |
If [ -z “$d” ] | If string is null |
If [ -n “$d” ] | If the string is not null // something’s in there. Don’t know what |
If [ “$d” != “hello” ] | If string does not equals to specific string |
Commands | Function |
If [ “$numb” -eq “12” ] | If number equals to 12 |
If [ “$numb” -ne “12” ] | If number does not equals to 12 |
If [ “$numb” -lt “12” ] | If number is lower than 12 |
If [ “$numb” -gt “12” ] | If number is greater than 12 |
If [ “$numb” -le “12” ] | If number is lower tor equal to 12 |
If [ “$numb” -ge “12” ] | If number is greater or equal to 12 |
Commands | Function |
If [ -e “$myfile” ] | If file is exist |
If [ -d “$myfile” ] | If file is exist and it’s a directory |
If [ -f “$myfile” ] | If file is exist and it’s a regular file // not directory and link |
If [ -s “$myfile” ] | If file is exist and has a length > 0 // non empty file |
Commands | Function |
If [ $numb -gt 3 -a $numb -lt 9 ] | Checks 2 logic with AND operator |
If [ $numb -gt 3 -o $numb -lt 9 ] | Checks 2 logic with OR operator |
If [ -s “$myfile” -a -x “$myfile”] | Checks 2 condition with AND : file is non empty and executable |
If [[ $numb -gt 3 && $numb -lt 9 ]] | MODERN WAY TO WRITE LOOPS WITH SYMBOLS AND condition |
If [[ $numb -gt 3 || $numb -lt 9 ]] | OR condition |
Commands | Function |
For i in {1..3}; do echo {command} done | Commands get executed by element of the loop |
For i in {1,”hey”,2,”CAT”,”END”}; do If [ “$i” == “CAT” ]; then Break fi done | To stop element getting executed in the list |
For (( i = 0; i < 25; i=i+1 )); do echo “command” done | Modern way to loop with numbers |
Commands | Function |
exit | To exit the current shell // you’ll lose variables once you exit the shell you are in. you have to open again and assign variables again |
Bash
| To enter subshell; Exit To get out of subshell and you’ll get back to main shell |
File create in subshell | Whatever file you create in the subshell will remain in the current shell after you exit from the subshell |
Executing subshell script from the current shell | When you write script it becomes subshell when you try to execute from the current shell (by ./somescript). Everything you have inside somescript remains in the subshell. And when you exit from the subshell you can’t access any variable from the current shell |
Commands | Function |
Source script.sh | To execute subshell script and retain values of it inside the current shell |
. script.sh | Alternative of using source command |
Three types of commands
Commands | Function |
Type cat | To know which type of command cat is o/p: cat is /bin/cat |
type cd | cd is a shell builtin |
Type lf | Lf is aliased to “ ls -Flha” |
Type type | Type is a shell builtin |
Commands | Function |
Alias ll=‘ls -l’ | Set alias for command you frequently use/want to use. F.e; you can set command “ls -l” to whatever name you want to give as an alias |
Alias abc=’ls ../’ | Abc is aliased to ls ../ |
Alias abc | To see the content of the command o/p: alias abc=’ls ../’ |
ls; cd; echo “hey” | Use 3 command in one line |
Alias cpl=’clear; pwd; ls -l’ | Use alias command for which you use command many times |
Alias | To see list of alias you set in the shell |
Unalias yellow | To unset alias |
Commands | Function |
Which cd | To know the path of cd command o/p: /usr/bin/cd |
ls -l $(which cd) | To store and use the previous output in the next command |
a=$(pwd) | Storing output of the command inside a variable |
Date | To see the date of today |
d=$(date) echo $d | To store the date value in the variable named d o/p: When you call $d anytime it will give you the initial static value of the date set initially |
$d ne $(date) | $(date) will give you current date and time |
Commands | Function |
Source ./script param1 | To access subshell directory // see the script for more info |
Why?
Commands | Function |
Pwd echo $? | It will return the last command’s status. If it executed correctly it will return 0, if not then return 1. Some also returns >1 number as an output |
Commands | Function |
echo $(( 3+5 )) = 8 echo $(( 3**2 )) = 9 echo $(( 10/3 )) = 3 | |
Bc 3+8 = 11 | To get into another environment type “bc” in the terminal |
Commands | Function |
Bc 10/3 == 3 scale=3 o/p: 3.333 | To solve this problem you have to use scale=2 // where 2 is decimal point you want to get result in // 3.33 // set the decimal value |
quit | To get out of that environment |
Bc -l | Set decimal point to default value // meaning you get result of above example is 3.3333333333 // you can set scale nonetheless |
3^3 = 27 3^(-2) = .11 | |
sqrt(16) = 4 | Square root of value |
3+ 5^2 -12*sqrt(18) = -18.47 | |
(10<20) || (1<2) | Will return 1. If both condition fails then it will return 0 |
(10<20) && (10<3) | Will return 0. |
Length(123.22) = 5 | How many digit the value has |
Scale(123.345) = 3 | Count how many decimal number the value has |
Bc -q | Open environment without licence // -q stands for quiet. // open your env, do your work quietly |
Commands | Function |
cat | If you type cat without any file, it will wait for your input ; you can close it with ctrl+c ctrl+d. // it will print whatever you write |
cat << EOF | Use to change/redirect input of cat command // “<<” stands for here document. // EOF is the symbol that is end of the file // last line will be EOF because program has to know the last line or where the file is going to finish |
<< | Meaning whatever you write; << will redirect tand goes inside the cat command(or whatever command you write) |
Bc << | Use << command with bc |
Commands | Function |
cat <<< “hello there” | cat will take input from the here string which is “hello there” |
s=”het there” cat <<< $s | Takes the input for set variable |
Bc <<< “3*4” | |
Read w <<< “hey there” | echo $w |
q=”3*7 +5” | Bc $q |
Commands | Function |
Shift Shift 4 | All the parameter are shifted by 1 // second parameter assigned to $1 in the script All the parameter are shifted by 4 // 4th parameter assigned to $1 in the script |
$0 | Will give you absolute path of the current directory where you calling the script |
Commands | Function |
Calc 3+4 | o/p: 7 |
Calc -p 5 12/7 | Use with -p stands for precision. How many decimal points you want with the result. o/p: 1.71428 |
Grep whatYouAreLookingFor nameOfTheFile // grep something abc.txt
Why: helps you extract data in file(ex: database file, data: username, password, age, email)
Why = print the whole file content
sed -n ‘1,3’p test.sh | Printing file lines |
sed “s/gautam/alien/g” test.sh | Replace a value in a file g = globally substitute |
awk “{print}” test.sh | Print the whole file content |
awk ‘/gautam/{print}’ test.sh | Print lines with “gautam” word |
curl = command in a shell script, which will hit the target endpoint and get the JSON data that that endpoint has
Sed
REGEX(regular expression) allows you to define the pattern in text using special or complicated syntax
Practice
Delimiter: commonly used /.
sed ‘s/I/Gautam/g’ file.txt sed -i ‘s/I/Gautam/g’ file.txt sed -i ‘.bak’ ‘s/I/Gautam/g’ file.txt | Globally replace I a word with Gautam To overwrite file To overwrite file in MAC |
sed ‘s/I/Gautam’ file.txt | Replace first I in line with Gautam |
sed ‘s/I/Gautam/2’ file.txt | Replace second I in line with Gautam |
Practice
Delimiter: spaces are delimiter by default
awk ‘{print}’ file.txt | To print the file content. This is same as using cat command |
awk ’{print $1}’ file.txt | To prints the first field of every line; increment number to print the line field ahead |
awk ‘{print $1,$3}’ file.txt | To print in combination of 1st and 3rd filed of every line |
awk ‘{print $NF}’ | Prints the last field with a caveat
|
awk -F’:’ ‘print $1,$7,’ /etc/passwd | meaningful usage when using combined -this will shows what shell your users are using |