User Tools

Site Tools


programming:bash:hello_world

Hello world !

The basics

  • Take a look at the linux help sheets
  • Open a terminal
  • Try it !

If you don't know what a command means, don't forget the linux help sheets or the really useful Manual pages directly on the terminal

man <your command>

Execute this commands to create a folder with your name to do some exercices

pwd
cd
ls
cd Documents/
ls -la
mkdir <your folder>
ls -la
cd <your folder>
pwd
ls

Exercice 1 : echo

  • Execute on the command line :
echo "Hello world"
  • Create your new file
vim ex1.sh
  • Write the lines below
#!/bin/bash
 
echo "Hello world"
  • Then press Esc
  • Write :wq
  • Make your code executable with :
chmod +x ex1.sh
  • Then run your program by typing in the terminal:
./ex1.sh

Exercice 2 : read

  • Create your new file
vim ex2.sh
  • Write the lines below
#!/bin/bash
 
echo "What's your first name ?"
read first_name
 
echo "What's your last name ?"
read last_name
 
echo "Hello $first_name $last_name! Pleased to meet you :)"
  • Then press Esc
  • Write :wq
  • Make your code executable with :
chmod +x ex2.sh
  • Then run your program by typing in the terminal:
./ex2.sh

Exercice 3 : same but different

  • Create your new file
vim ex3.sh
  • Write the lines below
#!/bin/bash
 
read -p "What's your first name ? " first_name
 
read -p "What's your last name ? " last_name
 
echo "Hello $first_name $last_name! Pleased to meet you :)"
  • Then press Esc
  • Write :wq
  • Make your code executable with :
chmod +x ex3.sh
  • Then run your program by typing in the terminal:
./ex3.sh

Exercice 4 : for

  • Create your new file
vim ex4.sh
  • Write the lines below
#!/bin/bash
 
echo -e "Hello !\nChoose the maximum number : "
read number
echo "Display of numbers from 1 to $number"
 
for x in $(seq 1 $number);
do
    echo "$x"
done
  • Then press Esc
  • Write :wq
  • Make your code executable with :
chmod +x ex4.sh
  • Then run your program by typing in the terminal:
./ex4.sh

Exercice 5 : if fi

  • Create your new file
vim ex5.sh
  • Write the lines below
#!/bin/bash
 
echo "Please choose a number between 1 and 10 : " 
read number
if [[ $number -gt 10 ]] || [[ $number -lt 0 ]]
then
	echo -e "\nI said between 1 and 10 !"
	exit 1
fi
 
echo "Please choose a second number between 10 and 20 : "
read second_number
if [[ $second_number -gt 20 ]] || [[ $second_number -lt 10 ]]
then
	echo -e "\nI said between 10 and 20 !"
	exit 1
fi
 
addition=$(($number+$second_number))
echo -e "The addition of the numbers is : $number + $second_number = $addition"
  • Then press Esc
  • Write :wq
  • Make your code executable with :
chmod +x ex5.sh
  • Then run your program by typing in the terminal:
./ex5.sh
programming/bash/hello_world.txt · Last modified: 2022/04/06 09:04 by ateixeira