using shift and positional parameters in Linux is important step. shift is a built in shell command. It takes a number as input. For example if the command is “shift 5” , it means it will shift the positional parameters to the left. But what are positional parameters?

positional parameters

Positional parameters are just like variables used or defined by shell. They are $0,$1,$2,$3,$4,$5,$6,$7,$8 and $9.  I think to get the meaning of positional parameters , we need a good example.

Let run a command on Linux terminal

  • #set i am wiki stack and like to know Linux command

      ( set command will assign a position to the sentence “i am wiki stack and like to know Linux”)

  • #echo $1  ( it will print i )
  • #echo $2  ( it will print am )
  • #echo $3 ( it will print wiki)

using shift and positional parameters in Linux

Let run a command on Linux terminal

  • #set i am wiki stack and like to know Linux command
  • #shift 5 ( it will shift the positional parameters to the left by 5)
  • #echo $1 ( it will print like )

using positional parameters as argument to the shell script

let us write a shell script which takes a file as input and make it executable.

  • Save the above script as make_execu.sh
  • Change permission by ‘chmod +x make_execu.sh’ command
  • Run using using #sh make_execu.sh filename
  • When we run the script using command ‘sh make_execu.sh filename’ on Linux terminal, the file filename would be set position 1 in shell positional parameters.
  • if [ -z “$1” ] , would test whether the argument to the shell script make_execu.sh is supplied or not.