Unix shell 脚本找出脚本文件所在的目录? [英] Unix shell script find out which directory the script file resides?

查看:48
本文介绍了Unix shell 脚本找出脚本文件所在的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我需要使用与shell脚本文件位置相关的路径运行脚本,如何将当前目录更改为与脚本文件所在的目录相同的目录?

Basically I need to run the script with paths related to the shell script file location, how can I change the current directory to the same directory as where the script file resides?

推荐答案

在 Bash 中,你应该像这样得到你需要的:

In Bash, you should get what you need like this:

#!/usr/bin/env bash

BASEDIR=$(dirname "$0")
echo "$BASEDIR"

这篇关于Unix shell 脚本找出脚本文件所在的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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