0

I am rsyncing directories. using the following.

source_location='/pi/data/2018/image_*.png'
echo $source_location;

Prints /pi/data/2018/image_*.png

mydate=$(date +'%Y');
source_location='/pi/data/$mydate/image_*.png'

Prints /pi/data/2018/image_1.png /pi/data/2018/image_2.png /pi/data/2018/image_3.png etc.

How do I escape the * or is there a better way?

user3525290
  • 1,557
  • 2
  • 20
  • 47
  • 1
    `source_location='/pi/data/'$(date +'%Y')'/image_*.png'` – anubhava Dec 17 '18 at 17:04
  • 2
    1. what is your desired output? 2. echo $source_location on the second option will not print out what you said it does. – jeremysprofile Dec 17 '18 at 17:04
  • I was assuming the second line would print out `/pi/data/2018/image_*.png` just as the first line did. – user3525290 Dec 17 '18 at 17:05
  • 1
    @user3525290: What you're trying to do? What's your expected output? Please make your query more clearer. – User123 Dec 17 '18 at 17:32
  • My expected outcome would both print lines match. when I use `source_location="/pi/data/$mydate/image_*.png"` printing out source_location prints all of my files in the directory `/pi/data/2018/image_1.png /pi/data/2018/image_2.png /pi/data/2018/image_3.png` instead of just printing the text `/pi/data/2018/image_*.png` – user3525290 Dec 17 '18 at 17:36
  • Please edit your question and check this if it helps: `source_location=$(ls /pi/data/${mydate}/image_*.png) && echo $source_location` – User123 Dec 17 '18 at 17:40
  • 2
    Unquoted parameter expansions are subject to pathname expansion. I don't see how your *first* example could produce the output you claim. – chepner Dec 17 '18 at 18:07
  • Either way, `declare -p source_location` or `printf 'source_location=%q\n' "$source_location"` will be more informative as to the variable's contents. Even the [POSIX spec for `echo`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html) advises against its use (see the APPLICATION USAGE and RATIONALE sections). – Charles Duffy Dec 17 '18 at 18:09

2 Answers2

0

If you want to store a series of filenames from a glob expansion, use an array:

mydate=$(date +'%Y');
source_location=( "/pi/data/$mydate/image_"*.png )
echo "The first file is ${source_location[0]}"
echo "All files are: " "${source_location[@]}"
that other guy
  • 116,971
  • 11
  • 170
  • 194
-2

Single quotes do not allow interpolation. Double quotes do.

$: touch /pi/data/2018/image_foo.png           # create a file
$: touch /pi/data/2018/image_bar.png           # create a file
$: source_location='/pi/data/2018/image_*.png' # a single-quoted literal
$: echo '$source_location'                     # a single-quoted literal
$source_location
$: echo "$source_location"                     # a double-quoted variable
/pi/data/2018/image_*.png
$: echo $source_location                       # unquoted variable, parsed before passed to echo
/pi/data/2018/image_foo.png /pi/data/2018/image_bar.png

$: mydate=$(date +'%Y');
$: source_location='/pi/data/$mydate/image_*.png' # a single-quoted literal
$: echo '$source_location'                        # a single-quoted literal
$source_location
$: echo "$source_location"                        # a double-quoted variable containing a single-quoted literal
/pi/data/$mydate/image_*.png
$: source_location="/pi/data/$mydate/image_*.png" # a double-quoted variable
$: echo "$source_location"                        # a double-quoted variable containing a double-quoted variable
/pi/data/2018/image_*.png
$: echo $source_location                       # unquoted variable, parsed before passed to echo
/pi/data/2018/image_foo.png /pi/data/2018/image_bar.png

Maybe you should post a more complete question regarding what you are trying to accomplish with what you've tried. Also, please search the site before you do - there are likely a lot of questions out there that are relevant.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • I tried this `source_location='/pi/data/$mydate/image_*.png'` prints `/pi/data/$mydate/image_*.png`. – user3525290 Dec 17 '18 at 17:29
  • @user3525290 you should not use single quotes if you want some substitution in the text; use double ones. Or, perhaps, try to construct your text step by step. – linuxfan says Reinstate Monica Dec 17 '18 at 17:43
  • I don't understand bash as well and maybe that is the problem. To me if i use `source_location='/pi/data/2018/image_*.png'` and print that to the screen it prints `/pi/data/2018/image_*.png` but when i introduce a variable `/pi/data/$mydate/image_*.png` it expands and prints all directories `/pi/data/2018/image_21*.png /pi/data/2018/image_2*.png etc` – user3525290 Dec 17 '18 at 17:49
  • 2
    The behavior of `echo $source_location` without quotes depends on whether that glob would actually match any files on the system where that test took place -- behavior it's rather critical to expose and discuss in any discussion of the matter. – Charles Duffy Dec 17 '18 at 18:04
  • Was actually just copy/pasting that line as boilerplate without really looking at it. You're absolutely right. Quoted them. – Paul Hodges Dec 18 '18 at 14:20