Reader small image

You're reading from  Linux Command Line and Shell Scripting Bible - Third Edition

Product typeBook
Published inJan 2015
PublisherWiley
ISBN-139781118983843
Edition3rd Edition
Tools
Right arrow
Authors (2):
Richard Blum
Richard Blum
author image
Richard Blum

Richard Blum has more than 25 years as a network and systems administrator, currently managing Microsoft, Unix, Linux, and Novell servers for a network with more than 3,500 users. He has developed online programming and Linux courses that he teaches to students worldwide.
Read more about Richard Blum

Christine Bresnahan
Christine Bresnahan
author image
Christine Bresnahan

Christine Bresnahan has worked in the IT industry for more than 30 years and is currently an adjunct professor of Python programming and Linux system administration classes at Ivy Tech Community College in Indianapolis. She is the co-author of Linux Bible, Eighth Edition, and Linux Command Line and Shell Scripting Bible.
Read more about Christine Bresnahan

View More author details
Right arrow

Being Shifty

Another tool you have in your bash shell tool belt is the shift command. The bash shell provides the shift command to help you manipulate command line parameters. The shift command literally shifts the command line parameters in their relative positions.

When you use the shift command, it moves each parameter variable one position to the left by default. Thus, the value for variable $3 is moved to $2, the value for variable $2 is moved to $1, and the value for variable $1 is discarded (note that the value for variable $0, the program name, remains unchanged).

This is another great way to iterate through command line parameters, especially if you don't know how many parameters are available. You can just operate on the first parameter, shift the parameters over, and then operate on the first parameter again.

Here's a short demonstration of how this works:

$ cat test13.sh
#!/bin/bash
# demonstrating the shift command
echo
count=1
while [ -n "$1" ]
do
  ...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Linux Command Line and Shell Scripting Bible - Third Edition
Published in: Jan 2015Publisher: WileyISBN-13: 9781118983843

Authors (2)

author image
Richard Blum

Richard Blum has more than 25 years as a network and systems administrator, currently managing Microsoft, Unix, Linux, and Novell servers for a network with more than 3,500 users. He has developed online programming and Linux courses that he teaches to students worldwide.
Read more about Richard Blum

author image
Christine Bresnahan

Christine Bresnahan has worked in the IT industry for more than 30 years and is currently an adjunct professor of Python programming and Linux system administration classes at Ivy Tech Community College in Indianapolis. She is the co-author of Linux Bible, Eighth Edition, and Linux Command Line and Shell Scripting Bible.
Read more about Christine Bresnahan