Using Variable With Space In It?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Variable With Space In It?
# 1  
Old 01-20-2009
Using Variable With Space In It?

Hey , I'm new to Bash and am having trouble with writing a script.

I have a variable and want to use it to create a tar file, but, I am having problems. Can anyone tell me what I'm doing wrong?


filestr=`date "+%y-%m-%d %H.%M.%S"`.tar
tar -cvf $filestr test.txt



So, basically, I'm creating a tar file based on the datetime in a specific format, but, the problem is that there is a space in the file name, so, the script fails during the tar creation.

Any ideas?
# 2  
Old 01-20-2009
Enclose the tar file name you derived with double quotes, should work

Code:
$ tar -cvf "$(date "+%y-%m-%d %H.%M.%S").tar" work/sni.py

# 3  
Old 01-20-2009
so in your example , it will be

Code:
$ filestr=`date "+%y-%m-%d %H.%M.%S"`.tar
$ tar -cvf "$filestr" test.txt

09-01-21 04.23.34.tar will be created.

# 4  
Old 01-21-2009
Quote:
Originally Posted by beefeater267
Hey , I'm new to Bash and am having trouble with writing a script.

I have a variable and want to use it to create a tar file, but, I am having problems. Can anyone tell me what I'm doing wrong?

Code:
 
filestr=`date "+%y-%m-%d %H.%M.%S"`.tar
tar -cvf $filestr test.txt


Quote "$filestr".
Quote:

So, basically, I'm creating a tar file based on the datetime in a specific format, but, the problem is that there is a space in the file name, so, the script fails during the tar creation.

Any ideas?

The best idea is Don't create files with spaces in their names.

The next important idea is to use the full year, not two digits:

Code:
filestr=`date "+%Y-%m-%d_%H.%M.%S"`.tar
tar cvf "$filestr" test.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Space in PATH variable

Hi All, I did a review of some threads to see if someone had come across this problem. My system is an oracle virtual box solaris 10 installed on a windows 10 system. Right now I am learning scripting and am just playing with the env variables. I am trying to update the PATH to... (9 Replies)
Discussion started by: bdby
9 Replies

2. Shell Programming and Scripting

How to remove tab space if any in a variable?

I have a variable sumOfJEOutputFile which is the output file of an SQL command which contains the output of that SQL. The output looks like below: ----------- 58 I am using following code to manipulate the output: (sed 1,2d $sumOfJEOutputFile > $newTemp1 | sed '$d' $newTemp1)... (4 Replies)
Discussion started by: Sharma331
4 Replies

3. Shell Programming and Scripting

Finding position of space in a variable

HI All, am trying to find the position of space in a variable, it is working for other characters other than space ulab="ulab1|ulab2" find_pos=`expr index $ulab '|'` echo $find_pos above code worked fine but below one says syntax error ulab="ulab ulab2" find_pos=`expr index $ulab ' '`... (2 Replies)
Discussion started by: ulab
2 Replies

4. UNIX for Dummies Questions & Answers

awk for variable with a space

Hi experts, why does $ echo "one two three" | awk '{$3="my tree"; print $0}' one two my tree work, but $ var="my tree" $ echo "one two three" | awk '{$3="'$var'"; print $0}' awk: {$3="my awk: ^ unterminated string does not work? How can the variable tha contains a space be... (2 Replies)
Discussion started by: GermanicGalore
2 Replies

5. Shell Programming and Scripting

Setting path variable with a space.

Hi I am using MKS Toolkit c shell. I am trying to set a path variable something like c:/Program Files/blah/blah so set path=(c:/Program Files/blah/blah) this, however, does not work as it splits this thing up into 'c:/Program' and 'Files/blah/blah'. Does anyone have any ideas on... (9 Replies)
Discussion started by: vas28r13
9 Replies

6. Shell Programming and Scripting

Preserve space in variable of AWK

This seems to be a stupid basic question, but I cant get the space to stick in the awk variable. I do use this command to grep a time range of the log file. cat /var/log/daemon.log | awk '$0>=from&&$0<=to' from="$(date +%b" "%e" "%H:%M:%S -d -24hour)" to="$(date +%b" "%e" "%H:%M:%S)" I now... (9 Replies)
Discussion started by: Jotne
9 Replies

7. Shell Programming and Scripting

appending space to variable

Hi I need to write a script where there the user enters 3 input parameter variable number the program should ask the user left or right if it is left , the number specified that many spaces should be added to the value in front of the value and saved in the samee variable itself and if it is... (5 Replies)
Discussion started by: viv1
5 Replies

8. Shell Programming and Scripting

how to merge two variable in one variable with space between them?

Dear all I had two separate variable. Now i want to make them merge into one new variable with space between them. Kindly suggest me. var1=dec 15 var2=10 i want var3=dec 15 10 My main aim is as below: op of date command: >date >Sat Dec 15 10:17:35 IST 2007 i want only Dec... (2 Replies)
Discussion started by: jaydeep_sadaria
2 Replies

9. Shell Programming and Scripting

space in variable

Hi, How to check if a variable is having spaces or empty in shell scripts. Please help esham (4 Replies)
Discussion started by: esham
4 Replies

10. Shell Programming and Scripting

How to trim space in output variable ?

Hi , I have a code like this: uid=scott password=tiger database=db01 cat >runid_val.sql<<-EOA SET ECHO OFF SET FEEDBACK OFF SET HEADING OFF SELECT trim(runid_seq.nextval) FROM dual; EXIT EOA echo `cat runid_val.sql` V_RUNID=`sqlplus -s $uid/$password@$database @runid_val.sql`... (5 Replies)
Discussion started by: vj_76
5 Replies
Login or Register to Ask a Question