Linux Reader
Access files and folders on Ext, UFS, HFS, ReiserFS, or APFS file systems from Windows

Pew Pew madafakas!

Access files and folders on Ext, UFS, HFS, ReiserFS, or APFS file systems from Windows

Bash: get script directory in Linux

Here you will find out:

  • about bash script directory
  • how to use it
  • when DiskInternals can help

Are you ready? Let's read!

About bash script directory

Any programmer knows how important directories are. Well, Linux operators also have to take directories very seriously. So let’s explain how to handle and manage your Bash script directories. Different computer users have their own ways of getting around with code; however, when writing Bash scripts, here are some possible ways to get your script directory.

$0 value and the first attempt

For the first attempt, use the $0 value. Bash scripts store executed command lines as $1, $2, $3 and so on. Thus, the $0 value is the prior element of the command. Also, when trying to get the Bash script current directory, you need to know about the dirname command.

What is the dirname command?

Simply put, when you run the dirname command, it echoes the last part of the name of the directory for a filename. For example, the command "dirname /5/7/9" will echo "/5/7", and "dirname ../5/7/9" will echo "../5/7".

Combine $0 and dirname

You can combine the dirname and $0 commands to retrieve the directory where a Bash script is saved. However, it may not be an absolute path, but a relative one; it all depends on how the script is called.

Here you’ll have something like this:

The example given above will save the script’s directory name as a variable for later use, after which it will then echo it and you’ll get:

Now, if the bash file is saved at /home/sam/bin/, the permissions will be altered to execute chmod 0700 ~/bin/ from /home/sam.

Example one: absolute path

Remember the dirname command? The command above will echo:

Example two: relative path



It will echo:


In the second example, "dirname $0" echoed bin because a relative path was used to run the script. This is not really advisable.

Why you need to return the absolute path

There is a need to return the absolute path because that’s where the bash script is located. Change the script’s directory and get the directory name:

When you save the above script as /home/sam/bin/, irrespective of how it is called, it will always echo:

However, you should know that, although the “change directory” command (cd) can be used, your Bash script won’t change its directory. Plus, further calls within the script will be relative to the current directory.

When you run the script above in /home/sam and call “PWD" in the next line, it will yet echo "/home/sam."

Do you have a dual-boot: Linux and Windows?

It is obviously difficult to access Linux files on Windows if you’re dual-booting on a PC. However, with the aid of DiskInternals Linux Reader, you can easily access Linux partitions on Windows.

This software is totally free and simple to use. Also, the interface looks almost the same as the Windows Explorer interface.

DiskInternals Linux Reader makes it possible to view files in your Ext4/3/2, UFS2, ReiserFS or Reiser4 partitions when you boot to Windows on your dual-boot PC.

Related articles

FREE DOWNLOADVer 4.13, WinUpgrade to PROFrom $29.95

Please rate this article.
3.73 reviews