Linux Shell中反引号``, 单引号'', 双引号"", $混用总结

Posted bella1102

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Shell中反引号``, 单引号'', 双引号"", $混用总结相关的知识,希望对你有一定的参考价值。

用一个小程序(argv.py)来演示下:

#!/usr/local/bin/python3
import sys

print(sys.argv has these, len(sys.argv), elements:, sys.argv)

 运行与结果:

# 首先是赋予执行权限
chmod 755 argv.py

#1 一个空格一个命令行参数
./argv.py I love Shell scripting.
sys.argv has these 5 elements: [‘./argv.py‘, ‘I‘, ‘love‘, ‘Shell‘, ‘scripting.‘]                     

#2 命令行所有参数被‘‘或者""引起来变成一个字符串
./argv.py ‘I love Shell scripting.‘ 
sys.argv has these 2 elements: [‘./argv.py‘, ‘I love Shell scripting.‘]
./argv.py "I love Shell scripting."
sys.argv has these 2 elements: [‘./argv.py‘, ‘I love Shell scripting.‘]

#3 反引号内的执行结果成为命令行参数
./argv.py `seq 1 5`
sys.argv has these 6 elements: [‘./argv.py‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]

#4 被‘‘引起来反引号内的内容不执行,整体直接变成一个字符串
./argv.py ‘`seq 1 5`‘
sys.argv has these 2 elements: [‘./argv.py‘, ‘`seq 1 5`‘]

#5 被""引起来,反引号内的执行结果变成一个字符串
./argv.py "`seq 1 5`"
sys.argv has these 2 elements: [‘./argv.py‘, ‘1\n2\n3\n4\n5‘]

 

#6 变量作为命令行参数
a=‘I love Shell scripting.‘
./argv.py $a
sys.argv has these 5 elements: [‘./argv.py‘, ‘I‘, ‘love‘, ‘Shell‘, ‘scripting.‘]

#7 被‘‘引起来,a变量自身变成一个字符串
./argv.py ‘$a‘
sys.argv has these 2 elements: [‘./argv.py‘, ‘$a‘]

#8 被""引起来,输出a变量的内容
./argv.py "$a"
sys.argv has these 2 elements: [‘./argv.py‘, ‘I love Shell scripting.‘]

#9 类似3
./argv.py $(seq 1 5)
sys.argv has these 6 elements: [‘./argv.py‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]

#10 类似4
./argv.py ‘$(seq 1 5)‘
sys.argv has these 2 elements: [‘./argv.py‘, ‘$(seq 1 5)‘]
 
#11 类似5
./argv.py "$(seq 1 5)"
sys.argv has these 2 elements: [‘./argv.py‘, ‘1\n2\n3\n4\n5‘]

 

以上是关于Linux Shell中反引号``, 单引号'', 双引号"", $混用总结的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本中反引号与$()的区别

Mysql中反引号和单引号的区别

JavaScript 中反引号 (`) 的使用

linux bash shell 中的单引号和双引号

请教Linux shell命令中双引号与单引号嵌套的问题

shell脚本中单引号和双引号的区别