Anthony Flores
Anthony Flores

Reputation: 187

Bash Shell: Cannot use variable $ as a path to run tar

This a really short question. But is there something syntatically wrong with placing a variable $example as an argument for tar in a bash file?

I have the file written as

//only portion that really matters
#!/bin/bash
...
tar -cvpzf $filename $backup_source

//here's the actual code
#!/bin/bash
backup_source="~/momobobo"
backup_dest="~/momobobo_backup/"
dater=`date '+%m-%d-%Y-%H-%M-%S'`
filename="$backup_dest$dater.tgz"
echo “Backing Up your Linux System”
tar -cvpzf $filename $backup_source
echo tar -cvpzf $filename $backup_source
echo “Backup finished”

//and heres the error
“Backing Up your Linux System”
tar: ~/momobobo: Cannot stat: No such file or directory
tar (child): ~/momobobo_backup/07-02-2013-18-34-12.tgz: Cannot open: No such file or directory
tar   (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
tar -cvpzf ~/momobobo_backup/07-02-2013-18-34-12.tgz ~/momobobo

Notice the "echo tar ...". When I copy and paste the output and run it in my terminal there is no problem taring the file. I'm currently running Xubuntu and I already did an update.

Upvotes: 1

Views: 5912

Answers (1)

that other guy
that other guy

Reputation: 123570

~ doesn't expand to your home directory in double quotes.

Just remove the double quotes:

backup_source=~/momobobo
backup_dest=~/momobobo_backup/

In cases where you have things you would want to quote, you can use ~/"momobobo"

Upvotes: 6

Related Questions