如何将当前工作目录设置为 Bash 中脚本的目录? [英] How can I set the current working directory to the directory of the script in Bash?

查看:25
本文介绍了如何将当前工作目录设置为 Bash 中脚本的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Bash 脚本.我需要当前工作目录始终是脚本所在的目录.

I'm writing a Bash script. I need the current working directory to always be the directory that the script is located in.

默认行为是脚本中的当前工作目录是我运行它的 shell 的目录,但我不想要这种行为.

The default behavior is that the current working directory in the script is that of the shell from which I run it, but I do not want this behavior.

推荐答案

#!/bin/bash
cd "$(dirname "$0")"

这篇关于如何将当前工作目录设置为 Bash 中脚本的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆