简体   繁体   中英

Shell scripting - spaces in variables?

Here's the problem I have with my bash script:

    date1=(`date -d @$startdate`)
    date2=(`date -d @$enddate`)

    touch --date="$date1" /tmp/newerthan
    touch --date="$date2" /tmp/olderthan

Since the words returned by the date -d command contain spaces, the only thing that gets processed by the touch --date="$var" command is the first word (which is "Mon", "Tue", "Wed"... etc), so it doesn't work properly.

How can I work around this problem and be able to get the subsequent lines to process the entire string?

You've used an array assignment in:

date1=(`date -d @$startdate`)

Either use plain backquotes (definitely not the preferred technique, though):

date1=`date -d @$startdate`

Or (much better) use $(...) :

date1=$(date -d @$startdate)

Or, at a pinch (there'd have to be a good reason), use this to copy the whole array into the argument to touch :

touch --date="${date1[*]}" /tmp/newerthan

You might want to consider double quotes around @$startdate , too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM