A simple shell script with options

Amalfitano
1 min readOct 15, 2018

In my life as sysadmin sometimes I had to write scripts with menus, whose appearance modify the script behaviour. This is not a black thread, is juts about some uses in our day-to-day. An easy way to do that is with a while loop and shift command.

The advantage of this manner to write menus in bash is that you have not to specify cases depending on parameter position, you just moves the parameters until there not more.

shift command

This command moves shell options right to left, for example, $4 to $3… when the parameter position is $1 shift removes it. This commands not apply with $0 cause this is shell name.

Let’s see:

#!/bin/shechowhile [ -n "$1" ]
do
case "$1" in
-a) echo "Option -a" ;;
-b) echo "Option -b" ;;
-c) echo "Option -c" ;;
*) echo "Not an option $1" ;;
esac
shift
done
Bash output

--

--