shell 多个引号冲突_Linux Shell中反引号``, 单引号'', 双引号"", $混用总结

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

#!/usr/local/bin/python3

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值