[LINUX] Handling of quotes in [bash]

background

I'm a new engineer who has just started writing bash. I'm coding ** "Oh, here single quote? Double? Is it necessary to surround it in the first place? Well, it doesn't matter if it moves ..." ** I'm always worried about that, so I decided to organize it again.

Conclusion

OK with the following recognition. Details will be explained individually later. コメント 2020-08-17 221526.png

Single quote''

If you enclose a string in single quotes, every character in the string loses its special meaning and is interpreted as its literal meaning.

# $HOME is a special shell variable that sets the home directory of the user running the shell

$ echo '$HOME' #Surround with single quotes
$HOME #As it is$Output as HOME

$ echo "$HOME" #Enclose in double quotes
/c/Users/guest.name #Expands to the value of a shell variable

$ echo '*' #Surround with single quotes
* #As it is*Is output

$ echo * #Do not quote anything
bin doc src #Expanded to the file name in the current directory

Double quote ""

If you enclose a string in double quotes, most characters lose their special meaning, much like single quotes. However, the special meanings of \ $ and `remain, so parameter expansion and command substitution are done. ** To prevent unexpected behavior, it seems better to enclose "\ $ variables" and "\ $ (command replacement)" in double quotes. ** **

#Parameter expansion
$ var='*** hello  world ***' #With a series of spaces*String containing

$ echo "$var" #Enclose in double quotes
*** hello  world *** #Displayed correctly

$ echo $var #Do not enclose in double quotes
bin doc src hello world bin doc src #Spaces are interpreted as delimiters and the filenames of the current directory are expanded before and after

#Command replacement
$ user='$(whoami)' #Surround with single quotes
$ echo "$user"
$(whoami) #Interpreted as just a string

$ user="$(whoami)" #Enclose in double quotes
$ echo "$user"
guest.name #The result of the whoami command is assigned

backslash\

If you put a backslash in front of a character, the one character immediately after the \ loses its special meaning and is interpreted as the literal meaning.

$ echo \$HOME  # $Put a backslash in front of
$HOME #As it is$Output as HOME

$ echo $HOME #I have to add a backslash
/c/Users/guest.name #Expands to the value of a shell variable

$ echo \*  # *Put a backslash in front of
*  #As it is*Is output

$ echo *  #I have to add a backslash
bin doc src  #Expanded to the file name in the current directory

References: Takenori Yamamori "[Revised 3rd Edition] Shell Script Basic Reference ── #! / Bin / sh can do this" Technical Review Company (2018/11/14)

Recommended Posts

Handling of quotes in [bash]
Handling of JSON files in Python
Handling of character code of file in IronPython
[Bash / linux] Notes in case of trouble
Bash in Jupyter
Handling json in python
Error handling in PythonBox
Hexadecimal handling in Python 3
Handling of absolute paths of os.path.join
[Personal memo] Auto-completion of bash
Partial in case of trouble
Handling sparse matrices in Scipy
List of nodes in diagrams
Equivalence of objects in Python
python> Handling of 2D arrays
Handling of python on mac
Comparison of data frame handling in Python (pandas), R, Pig
Variable parameter expansion of bash
Implementation of quicksort in Python
Handling of HSV color space lower and upper in OpenCV
Relative url handling in python
Easy setting of Firewalld in multi-zone
Pixel manipulation of images in Python
Handling of sparse tree-structured attributes (Python)
Judgment of holidays including holidays with bash
Error divided by 0 Handling of ZeroDivisionError
The story of participating in AtCoder
Implementation of login function in Django
Division of timedelta in Python 2.7 series
Features of pd.NA in pandas 1.0.0 (rc0)
Handle dates in Linux bash commands
MySQL-automatic escape of parameters in python
[Error handling] peewee.IntegrityError 1451 occurs in peewee
Install GoLang in goenv of anyenv
Implementation of life game in Python
Waveform display of audio in Python
Summary of various operations in Tensorflow
Chain of Responsibility pattern in Java
The story of the "hole" in the file
Handling timezones in Python (datetime, pytz)
The meaning of ".object" in Django
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Data handling 2 Analysis of various data formats
View the full path (absolute path) of a file in a directory in Linux Bash
Python: Preprocessing in machine learning: Handling of missing, outlier, and imbalanced data