简体   繁体   中英

How to insert a variable in a file name in bash?

I am currently learning bash and learned some basics.

Now that I want a variable name for a file (eg 'Backup_2020-04-24') it does not work.

I should also mention that I'm on a MacOS device

What it looks like:

variable = "test" tar -zcvf projects_${variable}.gzip testfolder

expected: gzip file with name projects_test in a folder named testfolder.

The gzip gets created in the test folder, however.. the name is 'projects_'.

Does anyone know why this is the case and can explain it to me? when echoing, the variable is not empty.

Excuse me if this post is formatted bad, this is my first post in here. Please don't hesitate to tell me what do do better next time

It might be the spaces around variable = "test" . Try variable=test instead? (This should really be a comment, but I don't have enough rep yet to comment).

Or if the value you want variable to take on is something that needs to be computed:

foo=$(date --date='@2147483647')
echo $foo
#> Mon Jan 18 19:14:07 PST 2038

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