Linux Basic Commands

Variables in shell script

Define and call variable inside shell script

#! /bin/bash
NAME="Pero Perić"
echo "Hi $NAME !"

 

Read variable from terminal input

#! /bin/bash
echo 'What is your name?'
read NAME
echo "Hi $NAME bok"

 

 

Read from command line

#! /bin/bash

# variables from commnd line $1 $2 $3
# call with: $source 05var_cl_input.sh john mary ana
echo "Inserted variables are:"
printf "$1 \n$2 \n$3 \n"

 

redeclaring input variable

#! /bin/bash

# variables from command line $1 $2 $3
# call with: $source 05var_cl_input2.sh john mary ana

#renameing varibles from input
FIRST=$1
SEC=$2
TH=$3

echo "Inserted names are:"
printf "$FIRST \n$SEC \n$TH \n"

 

 

Concate variables:

#! /bin/bash
FIRSTNAME="Pero"
LASTNAME="Marković"
echo "Hi $FIRSTNAME $LASTNAME !"
echo $FIRSTNAME $LASTNAME

 

Environment variables:

$ printenv    -lists all environment variables

#! /bin/bash

# environment variable
echo "path is: $PATH"
echo "user is: $USER"

 

 Variables can be:

  • local -inside a script or program
  • environment -global variable
  • shell -shell specific variables