I am new to bash scripting. I've got a script to ping multiple machines that "I" wrote :

for var in "$@"
        ping -c 1 -W 2 "$var" > /dev/null
        echo $var = $?

What I'd like to do is allow a glob as an input to ping multiple machines of similar names.

For instance: pingall machine[:01-06:] but I am rather lost on how to accomplish this.


globbing is another name for filename generation or pathname expansion.

That's the process that converts shell wildcards into the list of files that match them. It can only be used for existing files.

Here, you could use brace expansion instead:

pingall machine{01..06}

Which the shell expands to machine01, machine02... machine06 separate arguments regardless of whether files by that name exist in the current directory (it is not globbing).

That syntax is from zsh and is also supported by recent versions of bash and yash -o braceexpand.

With ksh93, you'd use:

pingall machine{1..6%02d}

though in this particular case, you could also use:

pingall machine0{1..6}

With the GNU seq command, you could also do (assuming the default value of $IFS):

pingall $(seq -f machine%02.0f 6)

Patterns expand only when matching file names. Maybe you can use brace expansion instead? Run the script with

pingall machine0{1..6}

which will be expanded to

pingall machine01 machine02 machine03 machine04 machine05 machine06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.