Read glob from command line in Bash

后端 未结 3 1177
失恋的感觉
失恋的感觉 2020-12-10 15:38

How do I read a glob in Bash from command line? I tried this and it only picks up the first file in the glob:

#!/bin/bash
shopt -s nullglob
FILES=$1
for f in         


        
3条回答
  •  孤街浪徒
    2020-12-10 16:03

    The reason it reads only the first file, is that the pattern /home/hss/* gets expanded before it is passed as an argument to your script. So your script does not see it as a pattern, but as a list of files, matching that glob.

    So, you need to call it like eugene y specified in his post:

    sh script.sh "/home/hss/*" 4 gz
    

    The quoting of $1 looks optional to me. It just makes the pattern to expand in for cycle rather than in assignment.

提交回复
热议问题