在Linux中运行Shell脚本有多种方法,以下是详细步骤和注意事项:
chmod +x your_script.sh
x
表示可执行)。ls -l your_script.sh
(应有 -rwxr-xr-x
)。bash your_script.sh
或(如果脚本使用其他Shell解释器):
sh your_script.sh # 使用系统默认Shell
zsh your_script.sh # 使用Zsh
./your_script.sh
./
表示当前目录,否则系统会从 $PATH
中查找。source your_script.sh
# 或
. your_script.sh
Permission denied
chmod +x your_script.sh
。Command not found
./
前缀。
bash
./your_script.sh # 正确
your_script.sh # 错误(除非脚本在$PATH中)
bash
#!/bin/bash # 确保脚本第一行指定解释器路径
which bash
。Bad interpreter
#!/bin/bash
但Bash不在该路径)。which bash
确认)。bash your_script.sh
)。-x
参数查看执行过程。
bash
bash -x your_script.sh
bash
./your_script.sh arg1 arg2
脚本内通过 $1
、$2
等获取参数。your_script.sh
)。chmod +x your_script.sh
。./your_script.sh
(需权限)bash your_script.sh
(无需权限)根据需求选择合适的方法即可!