系统相关
首页 > 系统相关> > Linux Shell中反引号``, 单引号'', 双引号"", $混用总结

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']

 

标签:Shell,中反,py,argv,sys,these,elements,Linux
来源: https://www.cnblogs.com/bella1102/p/11067144.html