dukevin
dukevin

Reputation: 23178

linux: Access a directory containing a space

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

Answers (5)

glenn jackman
glenn jackman

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

Seth Robertson
Seth Robertson

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

Tudor Constantin
Tudor Constantin

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

karmakaze
karmakaze

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

Mayank
Mayank

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

Related Questions