-2

The destination is environment variable,

destination=~/Desktop

d=`date +%F-%H%M%S`

filename=${destination}/${d}.{query}

touch $filename

when executing command,it report error. because ~ treat as a string.

the filename is ~/Desktop/2020-05-30-120306.json, ~ is not recognized.

So how to do, I need ~ to be recognized correctly

alanhg
  • 1
  • 1
  • 3
  • You can use `$HOME` instead of `~`, but I don't see how what you show doesn't work. `~` is only not expanded when quoted, but you're not quoting it, as far as I can tell. – Benjamin W. May 30 '20 at 04:22

1 Answers1

0

Here's a link. It describes two ways this can be done. The first of which using an eval (which is dangerous):

d=`date +%F-%H%M%S`

eval destination="~/Desktop" 
query="json" 
filename=${destination}/${d}.${query}

touch $filename

Second of which is safer and the recommended option.

d=`date +%F-%H%M%S`

destination="~/Desktop"
query="json"
filename=${destination/#\~/$HOME}/${d}.${query}

touch $filename

Not sure how you are setting the $destination so you may need to play around with it, but these both worked for me.