# shell-script
#!/bin/bash
# Author: Ankush Singal
# Date Created: 31/12/2020
# Last Modified: 31/13/2020
# Description:
# Create a backup in ~/bash_course folder of all files in the home directory
# Usage:
#backup_script
```echo "Hello, ${USER^}"```
``` echo "I will now backup your hime directory, $HOME" ```
```currentdir=$(pwd)```
```echo "You are running this script from $currentdir"```
```echo "Therefore, I will save the backup in $currentdir"```
- ```tar -cf $currentdir/my_backup_"$(date +%d-%m-%Y_%H-%M-%S)".tar $HOME/* 2>/dev/null```
```echo "Backup Completed Successfully"```
- exit 0
-- Permission Calculator
-- echo "$PATH"
--- vi ~/.profile
export PATH="$PATH:$HOME/bash_course/scripts"
-- The PATH variable tells the shell where to search for executable files -- We can make our scripts accessible system-wide by putting them into a folder covered by our PATH
numbers=0123456789
${parameters:offset:length}^C
echo ${numbers:0:7}
--- time=$(date +%H:%m:%S)
--- echo "Hello $USER, the time right now is $time"
x=4
y=2
echo $(( 4 + 2 ))
echo $(( $x + $y ))
echo ~
echo $PWD
echo month{1..12}
month1 month2 month3 month4 month5 month6 month7 month
#BAsh processes command lines - Tokenisation, Command Identification, Shell expansions, Quote removal, redirections
if [[ 2 -gt 1]]; then
echo "hello world"
fi
A simple command is just a list of words separated by spaces and terminated by either a new line or one of the other control operators available in bash
Word splitting can have some very significant effects on how your command lines are interpreted
Globbing: It is also known as filename expansion, is used to generate an alphabetically-sorted list of file names that match a certain pattern exactly. Any word on the command line that contains an unquoted *,? or [ will be interpreted as a pattern
ls *
ls file[ab].txt
ls file[abc][abc][abc].txt
ls file[a-g].txt
rm ~/Downloads/*.jpg
- Backslashes: \
- Single Quotes: ``
- Double Quotes " "
echo $name > "~/$out" echo $name > "$HOME/$out"
echo "$(ls *.txt)"
#!/bin/bash
echo "My name is $1"
echo "My home directory is $2"
echo "My favourite color is $3"
echo "The 10th argument is ${10}
- Command line arguments are information you give to your script from your command line. Each argument is separated by a spac
- Positional parameters are parameters set by the shell to store the value of each of these command line arguments
#!/bin/bash
echo $(( {2:-0}
-- replace with
echo
chmod 744 calculator.sh
./calculator.sh + 5 5 5 5 ${parameter:-word}
#!/bin/bash
echo "My name is $1"
echo "My home directory is $2"
echo "My favourite color is
$@: $1 $2 $3 ... $N..... Where N is the number of command line arguments provided
$*: $1 $2 $3...
- That
$@ and $ * allow you to access all the positional parameters that are provided to your bash script - The $@ expands to all the positional parameters provided to the script, but provides them as unquoted separate words , that are subject to word splitting
- "$@" stops subsequent word splitting and ensures the value of the positional parameters is exactly how they were provided by the user
echo $REPLY
read.... hello
Now when i type: echo $REPLY
it gives hellol