Bash scripting tutorial. Bash scripting introduction Hello World!

Bash scripting tutorial 1.

One of the most powerful tools to have as a Linux administrator is BASH scripting.

Example 1 Simple Hello World!

Create the script using a text editor of your choice, will use nano in this instance.

nano -w hello-world.sh

To execute the script

./hello-world.sh

The numbers in brackets are to denote the explanation but don’t include these in the script.

Code

#!/bin/bash    (01)

echo Hello World    (02)

1. Defines the shell the script is using.

2. Echo outputs what is inputted after it to stdout (standard output/what is outputted to the terminal)

Example 2 Hello you, using hard coded variable.

Code

#!/bin/bash     (01)

name=User    (02)

echo Hi $name Im Bash    (03)

1. Defines the shell the script is using.

2. Sets the variable $name to User

3. Echo displays whats inputted after, in this case the Hi string and the variable we set earlier $name

Example 3 Hello You, using a variable set by input from stdin (standard input/what you type in the terminal)

Code

#!/bin/bash    (01)

echo Hi Im Bash whats your name?    (02)

read name    (03)

echo Hello  $name    (04)

1. Defines the shell the script is using.

2. we know echo by now. If not it displays whats inputted after it wether string or variable

3. Reads the input of stdin (standard input/what is inputted to the terminal) and sets the $name variable to what was inputted by the user

4. Echo ouputs to stdout the string Hello and the variable $name

 

 

Bash Scripting, Linux , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

13 + five =