Class Not

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#link for the site :-

https://linuxconfig.org/bash-scripting-tutorial-for-beginners

#DATE - 28 JULY 2021


#TIME - 8:42 PM

-----------------------------------------------

To start with bash script you should know how the bash script start

to define the script interpreter enter the prefix with path of bash as show down

`#!/bin/bash`

way of executing script

-> Their are many way to run a script


giving it a `chmod +x` to make file executable by typing `./filename` to run or `$
bash filename.sh`

#RELATIVE VS ABSOLUTE PATH

#BASH SHELL SCRIPT

A very simple program to print hello world as all in other programing language

~> create a file name `filename.sh`


~> enter our program as shown below to output hello world

```
#!/bin/bash
echo "hello world !"
```
echo behave as print in bash

~>make it executable with the chmod cmd and then run it.
```

chmod +x filename.sh
./filename.sh

```

#VARIABLES

Creating a simple bash script

```
#!/bin/bash
greeting="Welcome"
user=$(whoami)
day=$(date +%A)
echo "$greeting back $user! Today is $day, which is the best day of the entire
week!"

echo "Your Bash shell version is: $BASH_VERSION. Enjoy!"


```

First, we have declared a variable greeting and assigned a string value Welcome to
it. The next variable user contains a value of user name running a shell session.
This is done through a technique called command substitution. Meaning that the
output of the whoami command will be directly assigned to the user variable. The
same goes for our next variable day which holds a name of today's day produced by
date +%A command.

The second part of the script utilises the echo command to print a message while
substituting variable names now prefixed by $ sign with their relevant values. In
case you wonder about the last variable used $BASH_VERSION know that this is a so
called **internal variable** defined as part of your shell.

-------------------------------------------------
# Quick Tip:

Never name your private variables using UPPERCASE characters. This is because
uppercase variable names are reserved for internal shell variables, and you run a
risk of overwriting them. This may lead to the dysfunctional or misbehaving script
execution.

===========
=================================
================================================

${parameter}

this is called parameter expansion


it is required bcz our variable $variable is followed by characters which are part
of its name.
` ${user}_home_backup_{$date}.txt`

&> is used to ouutput the stderr and stdout to file

#FUNCTION

You might also like