The small symbol reflects the big problem, the shell underline _ and the variable relationship.

Source: Internet
Author: User

I've written before. A problem was encountered when naming filenames automatically by date and time.

#! /bin/"Pleaseinput the filename:"  filenamedate3'2 days ago  ' +%y%m%/home//home/$filename'_'/ home/"$filename _$date3"

Output is three files

23:22 20130329
23:22 underline_20130329
23:22 underline_20130329

Isn't it supposed to be the same for all three, all in the form of a filename_date,

And then I switched to a dash,

#! /bin/"Pleaseinput the filename:"  filenamedate3'2 days ago  ' +%y%m%/home/${filename}-/home/$filename'-"  /home/"$filename-$date 3"

The output files are:

23:24 dash-20130329
23:24 dash-20130329
23:24 dash-20130329

If the auto-named file delimiter is not a problem with a dash, why is it so?

Later through study and study

The shell variable name is followed by a non-lowercase string (including the size string), a number or an underscore, or the escape character "\" or the variable name with curly braces. Otherwise, the shell in this case attempts to use filename_ as the variable name. The statement does not print anything (null or an empty string) unless there is an accidental case where $filename_ already exists, which is why the file 20130329 was output for the first time.

In the shell, the method of taking a variable value $varname syntax is actually a shorthand form of common syntax ${varname}, whether it is to avoid ambiguity, or to increase readability, the variable plus parentheses are a good habit, do not lazy not to add yo.

P.S. Examples of Simplicity
[[email protected] mybash]#A=a[[email protected] mybash]#b=b[[email protected] mybash]#Echo $a $bAb[root@Linux Mybash]# Echo $a _$bB[root@Linux Mybash]#Echo $a \_$bA_b[root@Linux Mybash]#Echo ${a}_${b} #推荐方法A_b[root@Linux Mybash]#echo "$a" _ "$b"A_b[root@Linux Mybash]#echo $a "_" $bA_b

The small symbol reflects the big problem, the shell underline _ and the variable relationship.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.