[英]Run a Bash script command in a specified folder
In a Bash script, is it possible to run a command in a specific folder? 在Bash脚本中,是否可以在特定文件夹中运行命令? For example, I'd like to create a script that prints the contents of the
/home
folder using ls
, no matter which directory the script is saved in. 例如,我想创建一个脚本,使用
ls
打印/home
文件夹的内容,而不管该脚本保存在哪个目录中。
Actually, you can use cd
to change directory in a script (but it'll not affect its parent shell). 实际上,您可以使用
cd
更改脚本中的目录(但不会影响其父shell)。 Try 尝试
#!/bin/bash
# myscript.sh
cd /home
pwd
ls
It'll print current directory (which is /home
) and list the content of it. 它会打印当前目录(
/home
)并列出其内容。 No matter what location of myscript.sh
is. 无论
myscript.sh
处于什么位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.