How to get the real path of the current script in Linux

How to get the real path of the current script in Linux

1. Get the real path of the current script:

#!/bin/bash
if [[ $0 =~ ^\/.* ]] #Judge whether the current script is an absolute path, matching all thens starting with /
  script=$0
else
  script=$(pwd)/$0
fi
script=`readlink -f $script` #Get the real path of the file script_path=${script%/*} #Get the directory where the file is located realpath=$(readlink -f $script_path) #Get the real path of the directory where the file is located echo $script
echo $script_path
echo $realpath 

2.readlink -f: Get the real file location of the symbolic link, and finally get the real file location of the symbolic link

3.basename: Get the file name of the file

4.dirname: Get the directory name of the file

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to enhance Linux and Unix server security
  • Detailed explanation of the difference between chown and chmod commands in Linux
  • Use the more, less, and cat commands in Linux to view file contents
  • Explanation of Linux kernel optimization configuration for high-concurrency nginx server
  • How to modify the default encoding of mysql in Linux
  • How to delete garbled or special character files in Linux
  • Introduction to the use and disabling of transparent huge pages in Linux
  • Detailed explanation of upgrading Python and installing pip under Linux
  • How to use for loop combined with if to find files in Linux Shell directory
  • Detailed explanation of using top command to analyze Linux system performance

<<:  React example showing file upload progress

>>:  Mysql delete duplicate data to keep the smallest id solution

Recommend

Semantic web pages XHTML semantic markup

Another important aspect of separating structure ...

Using Openlayer in Vue to realize loading animation effect

Note: You cannot use scoped animations! ! ! ! via...

Solution to IDEA not being able to connect to MySQL port number occupation

I can log in to MYSQL normally under the command ...

Detailed explanation of Linux system directories sys, tmp, usr, var!

The growth path from a Linux novice to a Linux ma...

Vue v-model related knowledge summary

​v-model is a Vue directive that provides two-way...

Tutorial on how to use profile in MySQL

What is a profile? We can use it when we want to ...

An article to understand the usage of typeof in js

Table of contents Base Return Type String and Boo...

Detailed tutorial for installing mysql 8.0.12 under Windows

This article shares with you a detailed tutorial ...

Usage of HTML H title tag

The usage of H tags, especially h1, has always bee...

mysql IS NULL using index case explanation

Introduction The use of is null, is not null, and...

mysql5.7.19 zip detailed installation process and configuration

MySQL v5.7.19 official version (32/64 bit install...

Web Design Tutorial (2): On Imitation and Plagiarism

<br />In the previous article, I introduced ...

Take you to understand the event scheduler EVENT in MySQL

The event scheduler in MySQL, EVENT, is also call...