Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the similarities and differences between Shell strings

Shulou Source: shulou.com Published: 2022-06-03 08:49:19 10月12日 Update

This article mainly introduces "what are the similarities and differences between Shell strings". In daily operation, I believe many people have doubts about the similarities and differences between Shell strings. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the similarities and differences between Shell strings?" Next, please follow the editor to study!

#! / bin/sh# tests various string comparison operations. In # shell, the difference between adding single quotation marks to the value of a variable, refreshing quotation marks and not adding them: it is irrelevant to the type, that is, it does not add quotation marks to become a string type, and # single quotation marks do not replace related quantities, such as not interpreting the $symbol as a variable reference, thus replacing it with the value of the corresponding variable. The double quotation marks replace the original value entered by # author:tenfyguoA= "$1" B = "$2" echo "# to determine whether the string is equal to if [" $A "=" $B "] Thenecho "[=]" fi# determines whether the string is equal or not, which is equivalent to the above = if ["$A" = = "$B"]; thenecho "[=]" fi# Note: the function of = = behaves differently in [[]] and [], as follows # if $a starts with "a" (pattern match), it will be true if [["$A" = a *]] Thenecho "[= = a *]]" fi# if $an equals a * (character match), then the result is trueif [["$A" = = "a *"]] Thenecho "= /" aweed / "" fi#File globbing (wildcard) and word splitting will occur, and the a* will automatically match to the corresponding file that starts with a. # if there is a file in the current directory: add_crontab.sh, the following output ok#if ["add_crontab.sh" = = a*]; then # echo "ok" # fiif ["$A" = = a*] Thenecho "[= a *]" fi# if $an equals a * (character match), then the result is trueif ["$A" = = "a *"]; thenecho "= /" averse / "" fi# string is not equal if ["$A"! = "$B"]; thenecho "[! =]" fi# string is not equal if [["$A"! = "$B"]] Thenecho "[! =]]" fi# string is not empty, length is not 0if [- n "$A"]; thenecho "[- n]" fi# string is empty. That is, the length is 0.if [- z "$A"]; thenecho "[- z]" fi# needs to be escaped, otherwise it is considered to be a redirect symbol if [$A / > $B]; thenecho "[>]" fiif [[$A >]; thenecho "[>]" fi so far, the study on "what are the similarities and differences between Shell strings" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Characters strings quotes similarities and differences learning variables symbols no beginning file more type result length help different original practical next function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Apple Shulou Tech Info Shulou Information Xiaomi