简体   繁体   English

在指定的文件夹中运行Bash脚本命令

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM