Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does bash shell get the absolute path of the current script

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to get the absolute path of the current script by bash shell". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how bash shell gets the absolute path of the current script.

Sometimes we need to know the absolute path of the currently executed output shell script, which can be implemented in dirname.

We know that dirname can get the path where a file is located. The purpose of dirname is:

The output has removed the name of the trailing "/" character part; if the name does not contain "/"

It shows "." (represents the current directory).

Here is the command line description for dirname:

As you can see from the above description, what is returned directly from dirname is not necessarily an absolute path, depending on whether the parameter supplied to dirname is an absolute path.

So the following code does not necessarily have an absolute path in SHELL_FOLDER

SHELL_FOLDER=$ (dirname "$0")

You need to use cd and pwd commands to get the absolute path where the script is located. This is the correct way to write it.

SHELL_FOLDER=$ (cd "$(dirname" $0 "); pwd)

If you find the above method troublesome, there is another way to get the absolute path of the script, which is with the help of the readlink command. Here is the command line description of readlink:

So with the readlink command, we can directly get the full-path file name of the $0 parameter, and then use dirname to get the absolute path:

SHELL_FOLDER=$ (dirname $(readlink-f "$0")) so far, I believe you have a better understanding of "how bash shell gets the absolute path to the current script", so you might as well do it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report