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

查看:77
本文介绍了如何在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.

默认行为是脚本中的当前工作目录是运行它的外壳程序的目录,但是我不希望出现这种情况.

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天全站免登陆