![]() ![]() In this article, we learned about two ways to get a filename from a full file path in Linux. $ filename=$īasically what this does is that: it shreds the part before the last ‘/’ in ‘ full path’ and just keeps the rest of the string, which is nothing but the filename. ![]() Now we will apply parameter substitution and store the value in another variable. Hence to use this method, we need to store the file path in a variable. Print File Names Using Bash Parameter Substitutionīash Parameter Substitutions are a way to modify a variable on the fly. $ basename -a /var/log/syslog /var/log/dpkg.log Use the -a an argument to pass multiple paths. Here we modify the script that lists the file to take a command line argument. Although it is not too difficult to get the filename from a full path, basename automatically does it for you and you don’t have to go through the trouble of parsing the file path, etc. The script can also take command line arguments. Switch -F will determine character you use for split (/ in this case). I found that for me, in most cases, the solution from the second strategy is good enough.The whole purpose of the basename command is to retrieve filename out of a file path. Now explanation: You list file of your choice and then use awk on it. As always, choosing the solution to use in your script is a tradeoff between complexity, performance and correctness. bash scripting environment is very flexible, resulting scenarios that even the final solution is wrong. Starting with a naive but inconsistent solution, and finishing with a more complex but pretty reliable one.Įven the final solution presented here isn’t perfect. I explored a 3-step evolution of solutions for the same problem. It is not robust if you cd to a different directory before calling the ~ $ cat bar.shĪlso watch out for $CDPATH gotchas.! Summary This iteration is pretty robust and consistent in the face of aliases, sourcing, and other execution variants. This is based on this excellent StackOverflow answer. ![]() ] & SOURCE="$DIR/$SOURCE"Įcho ~ $ ~ $ source ~ $ ~ $ source foobar/baz.sh # If $SOURCE was a relative symlink (so no "/" as prefix, need to resolve it relative to the symlink base directory The third and final solution adds symlink-resolution to the second ~ $ cat bar.shĭIR="$( cd -P "$( dirname "$SOURCE" )" & pwd )" The shell function $” )” & pwd )” to consistently get script directory in bash when no symlinks are involvedįinal attempt: resolve symlinks in $BASH_SOURCE From the bash documentation:Īn array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. ![]() Second attempt: use $BASH_SOURCEīASH_SOURCE is an array variable. In bash scripts, $0 is NOT guaranteed to store the path to the current script! This makes the cd command fail, and pwd return the current directory instead of the script directory. When sourcing a script, $0 contains -bash. One example of such failure is with sourced foobar $ source. Obviously, when this isn’t the case, the approach will fail. In the example above, that element was the path to the script, but this isn’t always the case. Note that $0 stores the first element of the executed command. ) is used to execute the commands in a subshell and capture the output. This works fine, both when running from the same directory, or from another ~ $ cat foo.sh Get full path of current directory (using pwd).Get directory component of $0 (using dirname).Given that, a valid strategy to get the script directory may be something like: In bash, $0 stores the first element of the executed ~ $ cat foo.sh Getting current script directory in bash is more complex than it may seem…Īs in many other programming languages, it is possible to access the command line and arguments. The main idea is to get script full path with readlink or realpath command and later extract directory path with dirname. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |