You are not logged in.

#1 2009-05-08 23:33:27

Dethredic
Member
Registered: 2009-01-24
Posts: 361
Website

Space in file name when using variables (bash)

So I have this:

BKP_WHERE=/home/phil/backup
BKP_FILE=`date +%e_%m_%y`_backup.tar.gz
echo $BKP_WHERE/$BKP_FILE

and that returns: /home/phil/backup/ 8_05_09_backup.tar.gz

Notice the space between those two variables. Is there any way to fix this?

Offline

#2 2009-05-08 23:41:47

HashBox
Member
Registered: 2009-01-22
Posts: 271

Re: Space in file name when using variables (bash)

Searching for bash trim in google returned this page, http://stackoverflow.com/questions/3697 … h-variable which seems to be mostly useless apart from maybe this solution:

var=`hg st -R "$path" | sed -e 's/  *$//'`

So I suppose you could write:

BKP_FILE="`date +%e_%m_%y | sed -e 's/ *$//'`_backup.tar.gz"

Or something to that effect. Good luck!

Offline

#3 2009-05-09 00:18:59

Dethredic
Member
Registered: 2009-01-24
Posts: 361
Website

Re: Space in file name when using variables (bash)

HashBox wrote:

Searching for bash trim in google returned this page, http://stackoverflow.com/questions/3697 … h-variable which seems to be mostly useless apart from maybe this solution:

var=`hg st -R "$path" | sed -e 's/  *$//'`

So I suppose you could write:

BKP_FILE="`date +%e_%m_%y | sed -e 's/ *$//'`_backup.tar.gz"

Or something to that effect. Good luck!

The BKP_FILE variable works correctly, it is just when I combine it with the other there is a space between them.

Offline

#4 2009-05-09 02:19:29

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 788
Website

Re: Space in file name when using variables (bash)

date's manpage describes the %e option as day of month space padded. That is where the space is coming from. It doesn't appear when the output is to a terminal though. Use %d instead:

BKP_WHERE=/home/phil/backup
BKP_FILE=`date +%d_%m_%y`_backup.tar.gz
echo $BKP_WHERE/$BKP_FILE

Edit: Ah it does appear in a terminal. I just hadn't noticed it.

Last edited by mikesd (2009-05-09 04:38:52)

Offline

#5 2009-05-09 03:11:11

jt512
Member
Registered: 2009-02-19
Posts: 262

Re: Space in file name when using variables (bash)

mikesd wrote:

date's manpage describes the %e option as day of month space padded. That is where the space is coming from. It doesn't appear when the output is to a terminal though. Use %d instead[.]

date +%-e_%m_%y should also work (ie, put a 'minus' after '%'.

Offline

#6 2009-05-09 04:47:48

mikesd
Member
From: Australia
Registered: 2008-02-01
Posts: 788
Website

Re: Space in file name when using variables (bash)

jt512 wrote:

date +%-e_%m_%y should also work (ie, put a 'minus' after '%'.

Ahh. Even better if the leading zeros are a problem.

Offline

#7 2009-05-09 10:24:41

Dethredic
Member
Registered: 2009-01-24
Posts: 361
Website

Re: Space in file name when using variables (bash)

jt512 wrote:
mikesd wrote:

date's manpage describes the %e option as day of month space padded. That is where the space is coming from. It doesn't appear when the output is to a terminal though. Use %d instead[.]

date +%-e_%m_%y should also work (ie, put a 'minus' after '%'.

worked great! Thanks!

Offline

Board footer

Powered by FluxBB