Reputation: 23178
In a script, I have this line
#!/bin/sh
log="${var}logs/console logs/since2_%m-%d-%Y.log" # <-- console logs has a space
how can I access this file?
putting quotes like:
log="${var}logs/"console logs"/since2_%m-%d-%Y.log"
cancels out the quotes around it, and escaping the quotes makes it try to find a file containing the character "
Upvotes: 1
Views: 546
Reputation: 246807
If you intend to have today's date in that filename:
log="$(date "+${var}logs/console logs/since2_%m-%d-%Y.log")"
touch "$log"
I'd recommend you use %Y-%m-%d
as that sorts both cronologically and lexically.
Upvotes: 0
Reputation: 31451
The problem is not what is quoted in the question. Here is an example script which works. Note the quotes around the USAGE of $log in addition to the definition. If you want further help, post the complete script or a minimal working subset which people can run to reproduce the problem.
#!/bin/sh
var=/tmp/
log="${var}logs/console logs/since2_%m-%d-%Y.log"
mkdir -p "$log"
rmdir "$log"
fortune | tee "$log"
echo ----
cat "$log"
Upvotes: 2
Reputation: 26861
the variable $IFS holds the field separator, which by default is space, so try with
oldifs="$IFS"
IFS="
"
log="${var}logs/console logs/since2_%m-%d-%Y.log"
# do whatever you want with $log now
IFS=$oldifs
Upvotes: 1
Reputation: 36144
The trouble you're having is probably where you use $log, you should probably be using "$log" to preserve the spaces.
Upvotes: 3
Reputation: 5728
I think log="${var}logs/console\ logs/since2_%m-%d-%Y.log"
should work. Try once
The idea is to escape the [SPACE]
Upvotes: -2