filename contains space and wildcard in a variable

前端 未结 3 1003
难免孤独
难免孤独 2021-01-07 11:35

I receive files which names contain spaces and change every week (the name contains the week number)

IE, the file for this week looks like This is the file - w

相关标签:
3条回答
  • 2021-01-07 11:47

    Use a bash array

    v=( /dir/This\ is\ the\ file - w*.csv )
    

    If there is guaranteed to be only one matching file, you can just expand $v. Otherwise, you can get the full list of matching files by expanding as

    "${v[@]}"
    

    or individual matches using

    "${v[0]", "${v[1]}", etc
    
    0 讨论(0)
  • 2021-01-07 11:49
    echo /dir/"This is the file - w"*.csv
    

    or — you almost tried that —

    echo /dir/This\ is\ the\ file\ -\ w*.csv
    
    0 讨论(0)
  • 2021-01-07 11:51

    First of all, you should not use the dollar sign in an assignment.

    Moreover, wildcard expansion is not called in an assignment. You can use process substitution for example, though:

    FILE=$(echo 'This is the file - w'*.csv)
    

    Note that the wildcard itself is not included in the quotes. Quotes prevent wildcard expansion.

    0 讨论(0)
提交回复
热议问题