Search code examples
shellposix

Put shell script for loop statement for multiple files in a function


#!/bin/sh

if [[ -d "$path" ]]; then
  cd "$path" && echo "$PWD"
  for file in *.jpg *.png *.jpeg *.gif *.gifv *.bmp; do 
    mv "$file" "$destpath"
  done
fi
  1. Since there are no arrays in POSIX defined. Can I put the *.jpg *.png *.jpeg *.gif *.gifv *.bmp; in a function? Because I am using these multiple times in a script.

  2. Is it possible to refactor cd "$path" && echo "$PWD" in my example? e.g. get rid of the cd command but still achieve the same effect? Thanks!


Solution

  • This one works for multiple file types

    for file in *.{jpg,jpeg,png,gif,gifv,bmp}; do
    done