Shift command help


#!/bin/bash

hostname=$1; shift

for hostname in $1

do

ping $hostname

done

I want to run the above script as hostname.sh yahoo.com google.com cnn.com. I want to shift each hostname to $1. How can do that with above code as currently it's not shifting.

No shifts necessary...

for hostname in "$@"
do	ping "$hostname"
done
1 Like

Thank you! But do you know how to do it with shift?

Yes. Why do you need to complicate things by using shift ?

Is this a homework assignment?

1 Like

No, just trying to make so I understand the shift command. I know I'm going to need it at some point.

You know that $# tells you how many positional parameters are present. Executing shift will decrement the value of $# as a side effect.

Can you come up with a while loop conditioned on the value of $# with the code in the loop using "$1" and the last command in your loop being a shift ?

1 Like