python允许直接使用命令行执行命令,例如: ```bash [yz@localhost ~]$ python -c "print('xyz')" xyz ``` 也可以多行,使用分号分割: ```bash [yz@localhost ~]$ python -c "xyz='xyz'; print(xyz)" xyz ``` 但是,如果需要(多行)缩减,写法有所不同。下面的写法会报错: ```bash [yz@localhost ~]$ python -c "xyz='xyz'; print(xyz); for i in range(3): print(i)" File "", line 1 xyz='xyz'; print(xyz); for i in range(3): print(i) ^ SyntaxError: invalid syntax ``` 这里,for循环因为需要使用缩减,单行无法表达缩进,因此,需要改换写法: ```bash [yz@localhost ~]$ python -c "xyz='xyz' > print(xyz) > for i in range(3): > print(i) > " ``` 运行结果: ``` xyz 0 1 2 ```