Xedni
Xedni

Reputation: 4715

Backing up file with cp fails when using variable

Little bit stumped here. I'm trying to write a simple bash function I can call to make a backup of my current ~/.bashrc file. The function basically does this:

backup_filename="~/.bashrc_`date +%Y%m%d`"
cp ~/.bashrc "$backup_filename"

when I run that however it says:

cp: cannot create regular file '~/.bashrc_20230913': No such file or directory

However if I run the same statement without the $backup_filename variable, it works just fine:

# This works no problem
cp ~/.bashrc ~/.bashrc_20230913

I've also tried using

cat ~/.bashrc > $backup_filename 

but it gives me basically the same error:

-bash ~/.bashrc_20230913: No such file or directory

I can't figure out what I'm doing wrong here. Can anyone help?

Upvotes: 0

Views: 23

Answers (0)

Related Questions