Filename not printing correctly with underscore “_

2020-01-25 02:05发布

I am using this

DATE_FOLDER=$(date +"%b-%d-%a-%G")
FILENAME="HOME_$date1.tar.gz"

echo $BACKUP_DESTINATION/$DATE_FOLDER/$FOLDERNAME_$FILENAME

My output is

home/May-04-Wed-2011/HOME_May-04-0718PM-2011.tar.gz

but if I use - instead of underscore _

echo $BACKUP_DESTINATION/$DATE_FOLDER/$FOLDERNAME-$FILENAME

then my ouput is correct

/home/May-04-Wed-2011/vmware-HOME_May-04-0717PM-2011.tar.gz

Why is that?

标签: linux bash
2条回答
Fickle 薄情
2楼-- · 2020-01-25 02:45

The problem is here : HOME_$date1.tar.gz and also here: _$FILENAME. If you use _$ then the $ is escaped.

Nevertheless you can do it with: \\_$. Then you escape the _ with the \ and the $ will be interpreted as you are used to it.

查看更多
SAY GOODBYE
3楼-- · 2020-01-25 02:53

_ is a valid character for a variable name, and $FOLDERNAME_ doesn't exist.

echo "$BACKUP_DESTINATION/$DATE_FOLDER/${FOLDERNAME}_$FILENAME"
查看更多
登录 后发表回答